Summer · Week 7 of 8
Advanced Classes
Enums, nested classes, ArrayLists, and wrapper classes — the patterns you'll see constantly in robot code.
Enums
An enum is a fixed set of named constants. Instead of using magic numbers or strings for states, enums make your code readable and safe — the compiler won't let you pass an invalid value.
We use enums constantly on 2974. Robot states, game piece types, scoring positions, arm positions — all enums. If you forget this week, you won't be able to read our codebase.
java
// Define the enum public enum RobotState { DISABLED, TELEOP, AUTO, TEST } // Use it RobotState state = RobotState.TELEOP; switch (state) { case TELEOP: runTeleopCode(); break; case AUTO: runAutoCode(); break; default: stop(); }
ArrayList
A resizable array. Unlike regular arrays, ArrayLists can grow and shrink. The tradeoff: they only hold objects, not primitives (you need wrapper classes for that).
java
import java.util.ArrayList; ArrayList<String> activeSubsystems = new ArrayList<>(); activeSubsystems.add("Drivetrain"); activeSubsystems.add("Shooter"); activeSubsystems.add("Intake"); System.out.println(activeSubsystems.get(0)); // "Drivetrain" System.out.println(activeSubsystems.size()); // 3 activeSubsystems.remove("Intake"); System.out.println(activeSubsystems.contains("Intake")); // false
| Feature | Array | ArrayList |
|---|---|---|
| size | Fixed forever | Grows/shrinks dynamically |
| types | Primitives + objects | Objects only (use wrappers) |
| syntax | arr[i] | list.get(i) |
| use when | Known fixed size | Dynamic, unknown size |
Wrapper Classes
ArrayLists can't hold primitives directly. Wrapper classes are the object versions of primitives.
java
// Wrappers for primitives ArrayList<Integer> ids = new ArrayList<>(); // not int ArrayList<Double> speeds = new ArrayList<>(); // not double // Java autoboxes automatically — int ↔ Integer ids.add(5); // int 5 auto-boxed to Integer int id = ids.get(0); // Integer auto-unboxed to int
Fill in the Blanks
// Declare a RobotState enum with TELEOP and AUTO
public RobotState { TELEOP, AUTO }
public RobotState { TELEOP, AUTO }
// Create an ArrayList of Strings
ArrayList<> names = new ArrayList<>();
ArrayList<> names = new ArrayList<>();
// Get the second element from list
String s = list.;
String s = list.;