Arrays in Java: Storing and Looping Through Many Values
Learn Java arrays from scratch — create them, access elements by index (starting at 0), use .length, loop through them, and avoid the ArrayIndexOutOfBoundsException.
So far, each variable has held a single value. But what if you need to store 30 students' marks, or a list of city names? You could make 30 variables… or you could use an array. An array stores many values of the same type in one place, under one name — and you reach each value by its position number. Arrays are your first real data structure, and they're everywhere.
What is an array?
Think of an array as a row of numbered boxes, all holding the same type of value. Here's one holding five test scores:
The single most important thing to burn into your memory: array positions (indexes) start at 0, not 1. So the first item is at index 0, and in an array of 5 items the last is at index 4. This trips up every beginner — expect it.
Creating an array
There are two common ways. If you already know the values, list them in curly braces:
int[] scores = {85, 90, 78, 92, 88};
String[] cities = {"Delhi", "Mumbai", "Pune"};
If you know the size but not the values yet, create an empty array with new:
int[] marks = new int[5]; // 5 slots, all default to 0
Reading and changing values
Use square brackets with the index to get or set a value:
int[] scores = {85, 90, 78, 92, 88};
System.out.println(scores[0]); // 85 (the FIRST element)
System.out.println(scores[2]); // 78
scores[1] = 95; // change index 1 from 90 to 95
The length of an array
Every array knows its own size via .length (note: no parentheses — it's not a method):
int[] scores = {85, 90, 78, 92, 88};
System.out.println(scores.length); // 5
Looping through an array
This is where arrays and loops become best friends. To visit every element, loop from 0 up to length:
int[] scores = {85, 90, 78, 92, 88};
for (int i = 0; i < scores.length; i++) {
System.out.println("Score " + i + ": " + scores[i]);
}
Notice the condition is i < scores.length (less-than, not less-than-or-equal). That's deliberate — it stops at the last valid index. Even cleaner is the for-each loop when you don't need the index:
for (int score : scores) {
System.out.println(score);
}
scores[5] in a 5-element array (valid indexes are 0–4) — Java crashes with this error. It's Java's way of saying "there's no box at that position." When you see it, check your indexes and remember: last index = length - 1.A quick real example: finding the highest score
int[] scores = {85, 90, 78, 92, 88};
int highest = scores[0];
for (int score : scores) {
if (score > highest) {
highest = score;
}
}
System.out.println("Highest: " + highest); // Highest: 92
This little "start with the first, then compare each one" pattern is one you'll reuse endlessly.
That off-by-one ArrayIndexOutOfBoundsException caught me more times than I'd like to admit early on — almost always from writing <= instead of < in my loop. Now it's automatic: index starts at 0, last index is length minus 1. Say it a few times and it sticks.
Key takeaways
- An array stores many values of the same type under one name; access them by index.
- Indexes start at 0 — the last one is
length - 1. - Get the size with
.length(no parentheses); loop withforor for-each. - Going out of range throws ArrayIndexOutOfBoundsException — check your bounds.
← Previous: Lesson 9 — Methods in Java
Next: Lesson 11 — Strings in Java →