Coding/Learn/java/array indexing
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnArray Indexing & Traversal
22 / 31 0%
Lesson 22 of 31
6 mins read

Array Indexing & Traversal

PreviousNext Lesson
In Plain English

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`.

The Mental Model

"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.

PointCore Rules to Remember

Point star
Zero-Based Indexing: Valid indices run from 0 to length - 1.
Point star
The length Field: Array property without parentheses (arr.length).
Point star
Out of Bounds Safety: Java throws ArrayIndexOutOfBoundsException on invalid index.

Common Beginner Traps & How to Fix Them

Mistake:Using i <= arr.length in loop condition

Why it happens: When i reaches arr.length, accessing arr[i] throws an ArrayIndexOutOfBoundsException.

How to fix: Always use strictly less than: i < arr.length.

Pro Tip: To get the last element in any non-empty array: arr[arr.length - 1].

Live Interactive Example

Hit Run Code to see it live
Traversing an Array with Index Loop
Java 13
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: The loop visits indices 0, 1, 2, 3 cleanly without throwing out-of-bounds errors.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate sum of array elements

  • PointGiven int[] values = {5, 10, 15};
  • PointLoop through the array, calculate total sum, and print "Total: " + sum.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates