← Back to Week 2 Hub

Arrays — Memory & Indexing

How Java stores collections of values in fixed-size, indexed containers

Two Ways to Create Arrays

With Initial Values

int[] scores = {85, 92, 78, 95, 88};
Values filled in at creation

With Fixed Size (empty)

int[] scores = new int[5];
int defaults to 0

String Array (empty)

String[] names = new String[3];
String defaults to null
Interactive Array Explorer
int[] ages = {63, 65, 60, 53, 58, 37, 35, 31};

Click any cell to inspect it

Click a cell to see its access expression
ages.length returns 8
Last valid index = length - 1 = 7
Common Operations

Read a Value

Enter an index and click Read

Write a Value

Enter an index and value, then click Write
The Off-by-One Trap

What happens when you access ages[8] on an 8-element array?

Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
Rule: Valid indices are 0 to length - 1. For an 8-element array, that means 0 through 7.
Looping Through an Array
for (int i = 0; i < ages.length; i++) {
    System.out.println(ages[i]);
}
Warning: Always use < length, never <= length! The last valid index is length - 1.
Tip: Arrays have a FIXED size. Once created, you cannot add or remove elements. We will learn about ArrayList later — it can grow and shrink.

Click cells, type indices, and step through the loop to explore how arrays work in Java.

← break vs continue for-each Loop vs for Loop →