All Tools

Java Array & ArrayList Explorer

Explore Java data collections interactively. Visualize arrays, ArrayLists, and 2D arrays with step-by-step animations, searching and sorting algorithms, and matching Java code. Covers 30-40% of the AP Computer Science A exam.

Visualization

0123456382743398210
Run an operation to begin
DefaultActive / ComparingDone / Sorted

Controls

Results

Step
0/0
Array Length
7
Data Type
int[]
Current Array
{38, 27, 43, 3, 9, 82, 10}

Java Code

1int[] arr = {38, 27, 43, 3, 9, 82, 10};
2// arr.length = 7
3// Array is ready to use

Reference Guide

Arrays in Java

A Java array is a fixed-size, ordered collection of elements of the same type. Once created, its length cannot change. Arrays use zero-based indexing and store elements in contiguous memory.

Declaring an array: int[] arr = new int[5]; creates an array of 5 integers (all initialized to 0). You can also use an initializer list: int[] arr = 1;

Accessing elements runs in O(1) time since arrays support direct index access. Attempting to access an index outside 0 to arr.length-1 throws an ArrayIndexOutOfBoundsException.

Traversal uses either a standard for loop (when you need the index) or an enhanced for loop (when you only need the values). Enhanced for loops cannot modify the array.

ArrayList Methods

ArrayList<E> is a resizable array implementation from java.util. Unlike arrays, its size grows and shrinks automatically. It can only hold objects (not primitives), so int values are autoboxed to Integer.

add(E e) appends to the end in amortized O(1). add(int index, E e) inserts at position and shifts subsequent elements right in O(n).

remove(int index) removes the element and shifts left in O(n). get(index) and set(index, value) are O(1) random access. size() returns the count and contains(Object o) does a linear search.

A common AP CSA pitfall is removing elements during an enhanced for loop, which throws ConcurrentModificationException. Traverse backwards with a standard for loop instead.

2D Arrays

A 2D array in Java is an array of arrays. int[][] grid = new int[3][4]; creates a 3-row, 4-column grid. Access elements with grid[row][col].

Row-major traversal uses nested for loops where the outer loop iterates rows and the inner loop iterates columns. This is the standard traversal order for AP CSA.

Column-major traversal swaps the loop nesting so columns are the outer loop. This visits all elements in column 0, then column 1, and so on.

Common 2D array applications include image processing (rotation, filters), matrix arithmetic, and game boards. The AP exam frequently asks about traversal order and nested loop patterns.

Searching & Sorting

Linear search checks each element one by one. It works on any array and runs in O(n) time. It is the only search option for unsorted data.

Binary search requires a sorted array. It repeatedly halves the search space by comparing the target with the middle element. Runs in O(log n) time.

Selection sort finds the minimum of the unsorted portion and swaps it into place. Always O(n^2) comparisons but at most O(n) swaps. Good when writes are expensive.

Insertion sort builds the sorted array one element at a time by inserting each new element into its correct position. O(n^2) worst case, but O(n) on nearly sorted data, making it efficient for small or partially sorted arrays.