Arrays
What arrays are
An array is a fixed number of slots, each addressed by an index starting at 0. The length is how many slots exist. Each stored value is an element. Arrays can hold primitives (int, double, boolean) or references (String, your own classes).
Creating arrays — basic types
// Arrays of primitive types
int[] sensorReadings = new int[5]; // Array for 5 integers
double[] coordinates = new double[6]; // Array for 6 coordinates
boolean[] motorStatus = new boolean[4]; // Array for 4 boolean values
// Initialize with values
int[] powerLevels = {25, 50, 75, 100};
double[] targetPositions = {12.5, 24.0, 36.5};
// Access elements using index (starts at 0)
sensorReadings[0] = 100; // First element
sensorReadings[4] = 95; // Last element (index 4 for array of size 5)
System.out.println("Array length: " + sensorReadings.length);
System.out.println("First reading: " + sensorReadings[0]);Arrays of objects
Object arrays work the same way. Each slot holds a reference (or null until you assign one).
String arrays example
// Create an array of strings
String[] teamMembers = new String[4];
// Add names to the array
teamMembers[0] = "Alice";
teamMembers[1] = "Bob";
teamMembers[2] = "Charlie";
teamMembers[3] = "Diana";
// Or initialize directly
String[] robotSystems = {"Drive", "Arm", "Intake", "Launcher"};
// Iterate through the array
System.out.println("Team Members:");
for (int i = 0; i < teamMembers.length; i++) {
System.out.println((i + 1) + ". " + teamMembers[i]);
}
// Enhanced for loop (for-each)
System.out.println("\nRobot Systems:");
for (String system : robotSystems) {
System.out.println("- " + system);
}Putting objects in arrays
You can assign an existing object or construct one in place.
Robot objects in arrays
// Assume we have a Robot class
public class Robot {
private String name;
private int teamNumber;
private boolean isActive;
public Robot(String name, int teamNumber) {
this.name = name;
this.teamNumber = teamNumber;
this.isActive = false;
}
public String getName() { return this.name; }
public int getTeamNumber() { return this.teamNumber; }
public boolean isActive() { return this.isActive; }
public void activate() { this.isActive = true; }
public String toString() {
return name + " (Team " + teamNumber + ") - " + (isActive ? "Active" : "Inactive");
}
}
// Create an array for robot objects
Robot[] competition = new Robot[4];
// Method 1: Create robot first, then add to array
Robot robot1 = new Robot("SpeedBot", 12345);
competition[0] = robot1;
// Method 2: Create robot directly in array assignment
competition[1] = new Robot("PowerBot", 67890);
competition[2] = new Robot("PrecisionBot", 11111);
competition[3] = new Robot("AllStarBot", 22222);
// Activate all robots and display
for (int i = 0; i < competition.length; i++) {
competition[i].activate();
System.out.println(competition[i]);
}User input for object arrays
Scanner input can fill an array when the size is not known until runtime.
Building object arrays from user input
import java.util.Scanner;
public class RobotTeamBuilder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many robots in your team? ");
int teamSize = Integer.valueOf(scanner.nextLine());
// Create array based on user input
Robot[] robotTeam = new Robot[teamSize];
// Collect robot information
for (int i = 0; i < robotTeam.length; i++) {
System.out.println("\nEntering Robot " + (i + 1) + ":");
System.out.print("Robot name: ");
String name = scanner.nextLine();
if (name.isEmpty()) {
System.out.println("Using default name: Robot" + (i + 1));
name = "Robot" + (i + 1);
}
System.out.print("Team number: ");
int teamNumber = Integer.valueOf(scanner.nextLine());
// Create and add robot to array
robotTeam[i] = new Robot(name, teamNumber);
}
// Display the complete team
System.out.println("\n=== Robot Team Roster ===");
System.out.println("Total robots: " + robotTeam.length);
for (int i = 0; i < robotTeam.length; i++) {
System.out.println((i + 1) + ". " + robotTeam[i]);
}
}
}Object array habits
- Null checks: Test elements before calling methods on them.
- Bounds: Valid indices are
0throughlength - 1. - Initialization:
new Robot[n]fills slots withnulluntil you assign. - For-each: Use when you do not need the index.
- Length: Arrays use
.length;ArrayListuses.size(). - Names: Name arrays after what they store (
motorPowers, notarr).
Try It Yourself:
Exercise 1: Complete Object Array System
- Create a Competition class and build an array of competitions
- Implement user input system for adding competitions with multiple parameters
- Create filtering system to find competitions by criteria
Build an FRC competition management system using object arrays: