Java Array Indexing & Traversal
Java arrays are 0-indexed: the first element is at index `0` and the last element is at `array.length - 1`. Attempting to access an index outside this range throws an `ArrayIndexOutOfBoundsException`.
"Think of hotel room floors in European numbering: Ground floor is index 0. The 4th room in a 4-room hallway is at slot 3. Trying to visit room 4 causes a "room does not exist" error."
Deep Dive: How It Works
The `.length` property (no parentheses) gives the total number of elements in the array.
Looping pattern: `for (int i = 0; i < arr.length; i++) { ... }`
Modifying in place: `arr[i] = arr[i] * 2;` directly updates the value in memory.
Syntax Blueprint
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}Condition i < arr.length safely stops before the out-of-bounds index.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using i <= arr.length in loop conditionWhy it happens: When i reaches arr.length, accessing arr[i] throws an ArrayIndexOutOfBoundsException.
How to fix: Always use strictly less than: i < arr.length.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate sum of array elements
Given int[] values = {5, 10, 15};
Loop through the array, calculate total sum, and print "Total: " + sum.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.