Collections
Store and work with multiple values.
Arrays
Arrays have a fixed size.
int[] numbers = {1, 2, 3, 4};Access an element using its index.
System.out.println(numbers[0]);Indexes start at 0.
ArrayLists
An ArrayList can grow or shrink as needed.
ArrayList<String> names = new ArrayList<>();Add values:
names.add("Alex");
names.add("Sam");Access values:
System.out.println(names.get(0));Remove values:
names.remove(0);Lists
You'll often declare an ArrayList using the List interface.
List<String> names = new ArrayList<>();This makes it easier to switch to a different list implementation later.
Looping Through a Collection
for (String name : names) {
System.out.println(name);
}FTC Example
You may store motors in a list to perform the same action on all of them.
for (DcMotor motor : driveMotors) {
motor.setPower(0);
}Key Takeaways
- Arrays have a fixed size.
- ArrayLists can change size.
- Lists store groups of related objects.
- Use loops to process collections efficiently.
Was this resource helpful?
