Home / Summer / Week 7
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
FeatureArrayArrayList
sizeFixed foreverGrows/shrinks dynamically
typesPrimitives + objectsObjects only (use wrappers)
syntaxarr[i]list.get(i)
use whenKnown fixed sizeDynamic, 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 }
// Create an ArrayList of Strings
ArrayList<> names = new ArrayList<>();
// Get the second element from list
String s = list.;

Knowledge Check