AP Computer Science A

Advanced Placement Computer Science A focusing on Java programming and object-oriented design.

Advanced Topics

Arrays and ArrayLists

Storing Many Values

Sometimes you need to keep track of lots of data—like scores, names, or objects. Arrays and ArrayLists are perfect for this.

Arrays

Arrays store a fixed number of elements of the same type.

int[] scores = {90, 85, 72, 100};
String[] names = new String[3];
  • Access elements with an index: scores[0] is 90.

ArrayLists

ArrayLists are flexible—they can grow and shrink as needed.

ArrayList<String> friends = new ArrayList<String>();
friends.add("Sam");
friends.add("Alex");
  • Use .get(index) to access elements: friends.get(0).

Choosing Between Them

  • Use arrays when the number of items is fixed.
  • Use ArrayLists for dynamic lists.

Real-Life Example

Arrays are like a row of lockers; ArrayLists are more like a backpack where you can add or remove things.

Examples

  • Storing the top 10 high scores in an int[] array.

  • Managing a dynamic list of online players with an ArrayList<String>.

In a Nutshell

Arrays and ArrayLists store multiple values efficiently.

Arrays and ArrayLists - AP Computer Science A Content | Practice Hub