Skip to main content

Lesson 6.4: Looping Through Hardware Arrays with for-each


Technical Context

Complex robots often utilize four drivetrain motors or multiple identical sensors. Iterating through these components individually is inefficient; using a for..each loop with a hardware array allows the programmer to apply a single command (like BRAKE behavior) to every component simultaneously, reducing the risk of accidentally missing a port.


Why for-each Loops Scale Better with More Hardware

A Java array is instantiated to hold a fixed number of SDK objects (e.g., DcMotor[] motors = new DcMotor[4]). The for..each syntax: for (Type variable : array): provides a shortcut to access every object in that array without managing an index integer, ensuring every connected component receives the instruction in a clean, readable block.

Two things to remember:

  • Arrays in Java are zero-indexed: the first element is at index 0.
  • The variable type declared in the for..each header must match the type stored in the array.

Annotated Code

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;

@TeleOp(name="Array_Drive_Demo")
public class ArrayDriveDemo extends OpMode {

// Array declaration: holds all four drivetrain motors
DcMotor[] allMotors;

private DcMotor leftFront, rightFront, leftBack, rightBack;

@Override
public void init() {
leftFront = hardwareMap.get(DcMotor.class, "left_front");
rightFront = hardwareMap.get(DcMotor.class, "right_front");
leftBack = hardwareMap.get(DcMotor.class, "left_back");
rightBack = hardwareMap.get(DcMotor.class, "right_back");

// Populate the array after mapping
allMotors = new DcMotor[]{ leftFront, rightFront, leftBack, rightBack };

// Apply BRAKE behavior to every motor in one loop
for (DcMotor motor : allMotors) {
motor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
}

@Override
public void loop() {
// Stop all motors at once using the array
for (DcMotor motor : allMotors) {
motor.setPower(0.0);
}
}
}

Fill-in-the-Blank Practice

  1. To declare an array of DC motors, the syntax is DcMotor__________ motors;.
  2. The index of the first element in any Java array is __________.
  3. In a for..each loop, the variable type must __________ the type of the objects stored in the array.
Show answers
  1. [] (e.g., DcMotor[] motors;)
  2. 0
  3. match

Simulator Challenge

Use the simulator below to practice looping across an array of motors and applying the same configuration to every element.

Telemark Unit 6 Simulator
Supports OpMode and LinearOpMode practice with lesson-specific starter code.
Includes loop stepping, runtime tracking, lifecycle controls, and live gamepad input.
Best for practicing loop structure, timers, and safe control flow patterns.

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.