Arrays

beginner60 min

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 objects

Object arrays work the same way. Each slot holds a reference (or null until you assign one).

String arrays example

Putting objects in arrays

You can assign an existing object or construct one in place.

Robot objects in arrays

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

Object array habits

  • Null checks: Test elements before calling methods on them.
  • Bounds: Valid indices are 0 through length - 1.
  • Initialization: new Robot[n] fills slots with null until you assign.
  • For-each: Use when you do not need the index.
  • Length: Arrays use .length; ArrayList uses .size().
  • Names: Name arrays after what they store (motorPowers, not arr).

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: