Skip to content
Adaptive

Learn Java Arrays and ArrayLists

Read the notes, then try the practice. It adapts as you go.When you're ready.

Session Length

~18 min

Adaptive Checks

16 questions

Transfer Probes

9

Lesson Notes

Arrays and ArrayLists are the fundamental data structures for storing collections of elements in Java. An array is a fixed-size, ordered collection of elements of the same type, declared with bracket notation (int[] arr = new int[5]) and accessed by zero-based index. Arrays support efficient random access but cannot be resized after creation.

ArrayLists are part of the java.util package and provide a resizable alternative to arrays. They store objects (not primitives), support dynamic sizing with methods like add(), remove(), get(), set(), and size(), and handle autoboxing for primitive wrapper types. Understanding when to use arrays versus ArrayLists is a key design decision in Java programming.

This topic also covers array traversal using standard for loops and enhanced for-each loops, common algorithms (searching, finding min/max, summing, reversing), 2D arrays for tabular data, and the important distinction between passing arrays by reference versus passing primitives by value. These concepts form the foundation of AP Computer Science A Units 6 and 7, comprising 10-15% of the AP exam.

You'll be able to:

  • Declare, initialize, and access elements in one-dimensional arrays using zero-based indexing
  • Traverse arrays using standard for loops and enhanced for-each loops to implement common algorithms
  • Create and manipulate ArrayLists using add, get, set, remove, and size methods
  • Explain autoboxing and unboxing between primitive types and wrapper classes
  • Declare and traverse 2D arrays using nested loops for row-by-row and column-by-column access

One step at a time.

Interactive Exploration

Adjust the controls and watch the concepts respond in real time.

Key Concepts

Array Declaration and Initialization

Arrays are fixed-size collections declared with a type and size. Elements are initialized to default values (0 for int, null for objects, false for boolean).

Example: int[] scores = new int[5]; // creates array of 5 ints, all initialized to 0 String[] names = {"Alice", "Bob", "Carol"}; // initializer list

Array Indexing

Array elements are accessed by zero-based integer index. The valid range is 0 to array.length - 1. Accessing an out-of-bounds index throws ArrayIndexOutOfBoundsException.

Example: int[] arr = {10, 20, 30}; arr[0] // returns 10 arr[2] // returns 30 arr[3] // throws ArrayIndexOutOfBoundsException

Array Traversal

Visiting every element of an array using a loop. Standard for loops use an index variable; enhanced for-each loops iterate directly over elements.

Example: for (int i = 0; i < arr.length; i++) { ... } // standard for (int val : arr) { ... } // enhanced for-each

ArrayList

A resizable collection from java.util that stores objects. Provides methods like add(), get(), set(), remove(), and size(). Requires import java.util.ArrayList.

Example: ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); names.get(0); // returns "Alice" names.size(); // returns 1

Autoboxing and Unboxing

Automatic conversion between primitive types and their wrapper classes (int <-> Integer, double <-> Double) when using ArrayLists, which cannot store primitives directly.

Example: ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(5); // autoboxing: int 5 -> Integer 5 int x = nums.get(0); // unboxing: Integer -> int

2D Arrays

Arrays of arrays used to represent tabular or grid data. Accessed with two indices: row and column. Declared as type[][] name = new type[rows][cols].

Example: int[][] grid = new int[3][4]; // 3 rows, 4 columns grid[1][2] = 7; // sets row 1, column 2 to 7

Linear Search

A sequential search algorithm that checks each element of an array one by one until the target is found or the end is reached. Time complexity: O(n).

Example: for (int i = 0; i < arr.length; i++) { if (arr[i] == target) return i; } return -1; // not found

Array vs ArrayList

Arrays are fixed-size and can hold primitives or objects. ArrayLists are resizable but can only hold objects. Arrays use [] syntax; ArrayLists use method calls.

Example: int[] arr = new int[3]; arr[0] = 1; arr.length; ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.size();

More terms are available in the glossary.

Explore your way

Choose a different way to engage with this topic β€” no grading, just richer thinking.

Explore your way β€” choose one:

Explore with AI β†’

Concept Map

See how the key ideas connect. Nodes color in as you practice.

Worked Example

Walk through a solved problem step-by-step. Try predicting each step before revealing it.

Adaptive Practice

This is guided practice, not just a quiz. Hints and pacing adjust in real time.

Small steps add up.

What you get while practicing:

  • Math Lens cues for what to look for and what to ignore.
  • Progressive hints (direction, rule, then apply).
  • Targeted feedback when a common misconception appears.

Teach It Back

The best way to know if you understand something: explain it in your own words.

Keep Practicing

More ways to strengthen what you just learned.

Java Arrays and ArrayLists Adaptive Course - Learn with AI Support | PiqCue