Arrays in Java — from Python lists to fixed size, indexes and references
I still remember the first time one of my Java programs stopped dead with a red message saying ArrayIndexOutOfBoundsException. It was a loop going through an array of grades, and I was convinced the code was perfect: I had written i <= grades.length. I came from Python, where a mistake like that would have given me an IndexError and that would have been it, but Java made me stop and understand why the last index is length - 1. It took me a good while to realise the problem wasn’t the loop, it was that I hadn’t understood what an array is underneath. This article is so it doesn’t happen to you.
We’ll see what an array is, how to create it, how to loop over it, why two “equal” arrays sometimes aren’t, and what the Arrays class does. If you’re coming from Python, we’ll compare with lists and tuples along the way, because that’s where half the confusion comes from. And if you haven’t yet seen how methods work, it’s worth going through Methods in Java first: we’ll use them at the end.
In a nutshell: An array in Java is a fixed-size collection of items of the same type, accessed by an index that starts at 0 (int[] marks = new int[5];). Its size can’t change after creating it, you loop over it with a for or a for-each and, since the variable holds a reference, you compare or copy arrays with Arrays.equals and Arrays.copyOf.
Table of Contents
Before you start. For this topic you only need to know what a typed variable is (int, double, String), how a for loop works and, for the last part, what a method is and how you pass data to it. The topic has four big ideas, in this order: what an array is and how to create it (numbered slots with a fixed size); how to use it (indexes, length and the two loops to go through it); the idea that changes everything, that the variable holds an address and not the data (that’s where copies, == and methods come from); and multi-dimensional tables together with the Arrays class. If you get lost at any point, go back to the egg carton: the whole topic can be told with it.
What an array is (and how it compares to a Python list)
Before the name: the five-grades problem
Before giving the definition, let’s see what problem it solves. Imagine you want to store the five grades of an exam and work out the average. With what you already know, you’d write five variables:
int grade1 = 7, grade2 = 5, grade3 = 9, grade4 = 4, grade5 = 10; double average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0;
It works, but with 30 students that would be 30 variables and, worst of all, you can’t go through them with a loop. A for gives you a number that keeps changing (0, 1, 2…), but there’s no way to tell Java “give me grade variable number i“. grade1 and grade2 are different names, not positions.
You need three things: a single name for all the grades, a way to pick each one with a number, and knowing how many there are. That’s exactly an array: one name (grades), a number for each slot (the index: grades[0], grades[1]…) and a size it knows by itself (grades.length).
And the size is fixed because Java reserves all the slots at once, like an egg carton that comes already made. Now, the definition.
An array is a collection of values of the same type, stored one after another, with a fixed size that is decided when you create it. Think of an egg carton: it has a set number of slots, they all hold the same kind of thing (eggs), and every slot has a position.
Compared with Python, keep these three differences in mind:
- All elements have the same type. An
int[]only holds integers. In a Python list you can mix numbers and text; here you can’t. - The size is fixed. There’s no
appendorremove. If you need more slots, you have to create a bigger array. (For a variable size, Java hasArrayList, which you’ll meet later in collections.) - Elements can be modified. In that sense it’s like a list, not like a tuple.
A summary to keep handy:
| Python list | Python tuple | Java array | |
|---|---|---|---|
| Mixed types | Yes | Yes | No, a single type |
| Changes size | Yes | No | No |
| Elements can be modified | Yes | No | Yes |
Declaring and creating an array
In Java you have to tell apart two steps that you don’t see in Python: declaring the variable (saying what kind of array it is) and creating the array (reserving the slots). The type is followed by square brackets:
int[] grades; // declaration: "grades" will be an array of integers (it doesn't exist yet)
There are two ways to create it. The first is to give the values directly, between curly braces:
int[] grades = {7, 5, 9, 4, 10}; // 5 slots, already filled in
The second is to give only the size with new, and fill it in afterwards:
int[] grades = new int[5]; // 5 slots, all of them are 0 grades[0] = 7; grades[1] = 5;
What happens on each line
Those lines do three different things, and it’s worth separating them because in Python they all happen at once without you seeing it:
| Step | What happens | Why |
|---|---|---|
int[] grades | The variable grades exists, but there’s no array yet | You’ve only said what type it will be; you haven’t asked for slots |
= new int[5] | Java makes 5 slots in a row, all set to 0, and grades starts pointing to them | new reserves memory; the 5 says how many slots |
grades[0] = 7; | The first slot goes from 0 to 7 | Index 0 is the first slot, just like in Python |
grades[1] = 5; | The second goes from 0 to 5; slots 2, 3 and 4 stay at 0 | Whatever you don’t fill keeps its default value |
With braces the same happens in a single step: Java counts the values, creates that many slots and fills them. That’s why you don’t write the size.
Check you’ve got it: what happens if you write int[] grades; and on the next line grades[0] = 7;?
It doesn’t compile. The compiler tells you variable grades might not have been initialized. You’ve declared the variable, but you never created the array, neither with new nor with braces, so there’s no slot 0 to store the 7 in. The creation step is missing: grades = new int[5]; before using it.
And what do the slots hold if I don’t fill them?
Java never leaves a slot “without a value”: when you create the array with new, every element gets the default value of its type:
| Element type | Default value |
|---|---|
int, long, short, byte | 0 |
double, float | 0.0 |
boolean | false |
char | the null character ('\u0000') |
String and any object | null (“points to nothing”) |
int[] integers = new int[3]; boolean[] flags = new boolean[3]; String[] names = new String[3]; System.out.println(integers[0]); // 0 System.out.println(flags[0]); // false System.out.println(names[0]); // null
A freshly created String array doesn’t contain empty strings: it contains null. If you try names[0].length() before assigning anything to it, you’ll get a NullPointerException. It’s a classic mistake.
Arrays of objects: the slots start as null
A very common search is how to make an “array of objects” in Java. You have already seen one without noticing: String is a class, so a String[] is an array of objects. You create it and loop over it just like an int[], with one difference worth keeping in mind: its slots hold arrows to objects, and an arrow that does not point to anything yet is null.
import java.util.Arrays;
public class ArrayOfObjects {
public static void main(String[] args) {
String[] students = new String[3]; // 3 slots for String: they all start as null
System.out.println(Arrays.toString(students));
students[0] = "Laura";
students[1] = "Pablo"; // slot 2 stays null
for (String s : students) {
if (s == null) { // with null, a.toUpperCase() would throw NullPointerException
System.out.println("(free slot)");
} else {
System.out.println(s.toUpperCase());
}
}
}
}
Output:
[null, null, null] LAURA PABLO (free slot)
The three slots start as null. We fill two of them and the third stays empty, which is why the loop asks if (s == null) before calling toUpperCase(): you cannot call any method on a null. When you get to Classes and objects in Java you will do the same with your own classes (Student[] group = new Student[30];), and it behaves exactly the same way: the array is born with 30 null values and each student has to be created with new before you use it.
new Student[30] does not create any student, only 30 empty slots. If you write group[0].getName() without first doing group[0] = new Student(...), the program stops with a NullPointerException.
Accessing elements: indexes and length
Just like in Python, indexes start at 0. In an array of 5 elements, the valid positions are 0, 1, 2, 3 and 4.
int[] grades = {7, 5, 9, 4, 10};
int first = grades[0]; // 7
grades[3] = 6; // now the array is {7, 5, 9, 6, 10}
int howMany = grades.length; // 5
int last = grades[grades.length - 1]; // 10
Notice that length has no parentheses: in Python you wrote len(list), here it’s grades.length. It’s an attribute of the array, not a method. (With String it’s the other way round: text.length() does have parentheses. One of Java’s little traps.)
And an important difference from Python: negative indexes don’t exist. In Python list[-1] is the last element; in Java, grades[-1] is an error. For the last one you have to write grades[grades.length - 1].
ArrayIndexOutOfBoundsException. If you ask for a position that doesn’t exist, the program stops:
Here it is drawn: five slots, indexes 0 to 4 and a sixth box that does not exist. The extra lap of <= lands right there.
grades[5] → Index 5 out of bounds for length 5
With 5 elements, the last index is 4, not 5. The rule: the last index is always length - 1. It’s exactly what happened to me with that loop; you’ll see it calmly in the next section.
Looping over an array
There are two ways, just like in Python: by index or by value.
By index: the classic for
int[] grades = {7, 5, 9, 4, 10};
for (int i = 0; i < grades.length; i++) {
System.out.println("Position " + i + ": " + grades[i]);
}
What matters is the condition: i < grades.length, with strictly less than. The last iteration happens with i = 4, and after adding one, i is 5 and the loop stops. If you write i <= grades.length, there’s an extra iteration with i = 5 and the exception from before appears.
The for loop traced, iteration by iteration
Let’s follow the loop with grades = {7, 5, 9, 4, 10} without skipping anything. In each iteration three things happen, always in this order: the condition is checked, if it holds the body runs and, when the body ends, i++ is done.
| Iteration | i | i < 5? | What it prints | Why |
|---|---|---|---|---|
| 1 | 0 | Yes | Position 0: 7 | It starts at 0 because that’s the first index |
| 2 | 1 | Yes | Position 1: 5 | At the end of iteration 1, i++ leaves i at 1 |
| 3 | 2 | Yes | Position 2: 9 | Each iteration adds one |
| 4 | 3 | Yes | Position 3: 4 | It’s still less than 5 |
| 5 | 4 | Yes | Position 4: 10 | 4 is the last valid index (length - 1) |
| (none) | 5 | No | nothing: the loop ends | 5 is not less than 5, so there’s no sixth iteration |
The important row is the last one: i does reach 5, but only to check the condition, never to read grades[5]. With <=, that check would be “5 <= 5?”, it would say yes and the body would try to read grades[5]: that’s where Index 5 out of bounds for length 5 jumps out, after correctly printing all five positions.
Count the iterations without running anything. A for (int i = 0; i < n; i++) does exactly n iterations, from 0 to n - 1. If it starts at 0 and uses <=, it does n + 1: one too many.
The most common mistake when looping over arrays: writing <= instead of < in the for condition. The program compiles without any problem and fails when it runs, right on the last iteration.
By value: the for-each
If you only need to read each value and you don’t care about its position, Java has a shorter version, equivalent to Python’s for value in list:
int sum = 0;
for (int grade : grades) { // "for each grade in grades"
sum += grade;
}
System.out.println("Sum: " + sum);
You read it as: “for each int called grade inside grades“. The colon : replaces Python’s word in.
Which one do I use? If you need the index (to know the position, to walk two arrays at once, or to modify elements), use the classic for. If you’re only going to read the values, use the for-each. It has a catch: in a for-each over int, the variable grade is a copy of each value, so doing grade = 0 inside the loop doesn’t change the array.
The point that confuses people most: an array is a reference
This is the hardest thing at first and, at the same time, what will save you the most mistakes if you understand it well. Look at this code and think about what you expect it to print:
int[] grades = {7, 5, 9};
int[] copy = grades; // does this copy the array?
copy[0] = 99;
System.out.println(grades[0]); // 7 or 99?
It prints 99. And it isn’t a bug: it’s how arrays work. A variable of type int[] doesn’t hold the numbers: it holds the address where the array is in memory. It’s like the address of a house. When you write int[] copy = grades;, you copy the address, not the house. Now there are two variables pointing to the same house, and if someone moves the furniture, both of them see it.
Line by line: what’s in memory
Go through the code above slowly, looking at how many arrays exist on each line and where each variable points. We’ll call the first one “house A”.
| Line | grades points to | copy points to | Arrays that exist | Why |
|---|---|---|---|---|
int[] grades = {7, 5, 9}; | house A | (doesn’t exist yet) | 1: A = {7, 5, 9} | The braces create a new array and grades stores its address |
int[] copy = grades; | house A | house A | 1 | There’s no new and no braces: nothing is created, only the address is copied |
copy[0] = 99; | house A | house A | 1: A = {99, 5, 9} | You go to copy‘s house, which is A, and change its slot 0 |
println(grades[0]) | house A | house A | 1 | You go to grades‘s house, which is also A, and its slot 0 is already 99 |
The tip so you never get it wrong: a new array only appears with new or with braces (or with a method that makes one, like Arrays.copyOf). If a line has none of those, no array has been created: at most, an address has been copied.
Check you’ve got it: after int[] a = {1, 2}; int[] b = a; b = new int[2]; b[0] = 9;, what is a[0]?
It’s 1. Up to int[] b = a;, both variables point to the same house. But b = new int[2]; makes a new array (there’s a new) and b starts pointing to that one. From then on, b[0] = 9 changes the new house, and a‘s house is still {1, 2}. Changing a slot is seen by anyone who shares the house; changing the whole variable only moves its own arrow.
With plain numbers it doesn’t happen: int b = a; copies the value and from then on they’re independent. Arrays (and all objects) behave differently. If you want a real copy, you have to create another array:
import java.util.Arrays;
int[] grades = {7, 5, 9};
int[] copy = Arrays.copyOf(grades, grades.length); // new, independent array
copy[0] = 99;
System.out.println(grades[0]); // 7 (the original doesn't change)
System.out.println(copy[0]); // 99
The drawing sums up both situations: on the left, copy = grades copies the arrow and both variables point to the same array; on the right, Arrays.copyOf builds a new array and each variable has its own.
Why == doesn’t work to compare arrays
The same idea explains another stumble. The == operator compares addresses, not contents:
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b); // false (they are two different arrays)
System.out.println(Arrays.equals(a, b)); // true (same contents)
It’s the same story as == versus .equals() with String: to compare contents, you use a method. For arrays, Arrays.equals(a, b).
And if I print an array directly, what comes out?
int[] grades = {7, 5, 9};
System.out.println(grades); // [I@49476842 (not the numbers!)
You get something like [I@49476842: the array’s type ([I means “array of int”) and a memory address. To see the contents, use Arrays.toString(grades), which gives [7, 5, 9].
Printing an array with println(array). If you see something like [I@6d06d69c, it isn’t a bug in your program: you just need Arrays.toString(...).
This idea of “the variable holds an address” will come back with classes and objects: String, and any object you create, work the same way. If you understand it here with arrays, you’re already halfway through that topic.
Arrays and methods
What we’ve just seen has a direct consequence if you already know methods: when you pass an array to a method, the address is passed, not a copy. That is, the method can modify your original array.
static void raiseByOne(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i]++; // modifies the original array
}
}
public static void main(String[] args) {
int[] grades = {7, 5, 9};
raiseByOne(grades);
System.out.println(Arrays.toString(grades)); // [8, 6, 10]
}
With a single int this doesn’t happen (the method receives a copy of the value). With an array, it does. It’s very useful, but also a source of mistakes: if a method “sorts” or “changes” your array without warning, you’ll be surprised later. A good habit is to make it clear in the method name whether it modifies or only calculates (raiseByOne modifies; average only calculates).
Java always passes a copy: what changes is a copy of what
You may have read that “arrays are passed by reference”. What really happens is simpler: Java always passes the method a copy of what’s in the variable. With an int, the variable holds the number, so the method gets a copy of the number. With an array, the variable holds the address, so the method gets a copy of the address: a different arrow, but to the same house.
That difference shows up as soon as the method tries to change the arrow instead of the slots:
static void setToZero(int[] data) {
data = new int[data.length]; // data now points to a new house, full of zeros
}
public static void main(String[] args) {
int[] grades = {7, 5, 9};
setToZero(grades);
System.out.println(Arrays.toString(grades)); // [7, 5, 9]: nothing has changed
}
Step by step: when the method is called, data gets a copy of grades‘s arrow, and both point to {7, 5, 9}. Inside, data = new int[...] makes a new house and moves only data‘s arrow; grades‘s arrow stays where it was. In raiseByOne, on the other hand, the code did data[i]++: it went into the shared house and changed its slots, and main does see that.
Trying to replace the outside array by assigning another one inside the method (data = …). It doesn’t work: to change values, go through the slots; to hand back a new array, return it with return.
Look at the drawing: during the call there are two variables, grades in main and data in the method, but only one array. That is why data[i]++ shows up in main without any return.
Multi-dimensional arrays
In Python, a table is represented with a list of lists. In Java, with an array of arrays, declared with one pair of square brackets per dimension:
int[][] table = {
{10, 15, 20, 30, 40},
{12, 14, 16, 18},
{11, 17, 23, 29, 31}
};
System.out.println(table[1][3]); // 18 → row 1, column 3
The way to think about it: table is an array of 3 elements, and each element is itself an array of integers. That’s why table[1] is the whole second row, and table[1][3] is element 3 of that row.
How to read the brackets: left to right
With two pairs of brackets, read from left to right and ask yourself at each step what you’re holding:
| Expression | What it is | Type | Value | Why |
|---|---|---|---|---|
table | The whole table | int[][] | 3 rows | It’s the outer array: its slots are rows |
table.length | How many rows there are | int | 3 | The outer array’s length counts rows, not numbers |
table[1] | Row 1 (the second one) | int[] | {12, 14, 16, 18} | The first bracket picks a slot of the outer array, and that slot is an array |
table[1].length | How many numbers that row has | int | 4 | Now length applies to the row, not to the table |
table[1][3] | Position 3 of row 1 | int | 18 | The second bracket picks inside the row: 12, 14, 16, 18 |
Look at the “Type” column: each index you add takes one pair of brackets off the type. int[][] with one index gives int[], and with two it gives int.
Check you’ve got it: with the same table, what do table[0].length, table[2][4] and table[1][4] give?
table[0].length is 5, because row 0 is {10, 15, 20, 30, 40}. table[2][4] is 31: row 2 is {11, 17, 23, 29, 31} and its position 4 is the last one. table[1][4] doesn’t exist: row 1 only has 4 numbers (indexes 0 to 3), so the program stops with Index 4 out of bounds for length 4. Each row has its own length.
The drawing shows the whole idea: table is an array of 3 arrows and each arrow leads to a row with its own length.
You can also create an empty table by giving rows and columns:
int[][] table = new int[2][3]; // 2 rows, 3 columns, everything at 0 table[1][2] = 5; System.out.println(Arrays.deepToString(table)); // [[0, 0, 0], [0, 0, 5]]
Notice that to print a table you use deepToString (“deep”), because the plain toString would only know how to show the addresses of the rows.
Since each row is an independent array, rows can have different lengths (in the example above, 5, 4 and 5 elements). That’s why, when looping, the length of each row is asked with table[i].length and not with a single common length.
Looping over a table
You need two nested loops: the outer one goes through the rows, the inner one goes through the elements of each row.
// By index
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
System.out.print(table[row][col] + " ");
}
System.out.println(); // new line after finishing each row
}
// By value
for (int[] row : table) { // each "row" is an int[]
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
In the for-each, the outer variable must be of the type one level down: if table is int[][], each row is an int[]. It’s the only rule to remember so you don’t get the types wrong.
The Arrays class: ready-made tools
Java comes with a class called Arrays (in the java.util package, which is why you have to write import java.util.Arrays; at the top of the file) with very common operations that aren’t worth programming by hand. The main ones:
| Method | What it does |
|---|---|
Arrays.toString(a) | Returns the contents as text: "[10, 15, 20]" |
Arrays.sort(a) | Sorts the array from smallest to largest (modifies the original) |
Arrays.binarySearch(a, x) | Looks for x in an already sorted array and returns its position |
Arrays.copyOf(a, n) | Creates a new array of length n with the data from a |
Arrays.copyOfRange(a, from, to) | Copies a slice (to is not included) |
Arrays.equals(a, b) | Compares the contents of two arrays |
Arrays.fill(a, x) | Fills the whole array with the value x |
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] data = {80, 70, 60, 50, 40, 30, 20, 10};
Arrays.sort(data);
System.out.println(Arrays.toString(data));
// [10, 20, 30, 40, 50, 60, 70, 80]
System.out.println(Arrays.binarySearch(data, 30)); // 2 (it's at position 2)
System.out.println(Arrays.binarySearch(data, 35)); // -4 (it's not there)
int[] slice = Arrays.copyOfRange(data, 2, 5);
System.out.println(Arrays.toString(slice)); // [30, 40, 50]
int[] bigger = Arrays.copyOf(data, 10); // the new slots are filled with 0
System.out.println(Arrays.toString(bigger));
// [10, 20, 30, 40, 50, 60, 70, 80, 0, 0]
int[] sevens = new int[5];
Arrays.fill(sevens, 7);
System.out.println(Arrays.toString(sevens)); // [7, 7, 7, 7, 7]
}
}
Why does binarySearch return -4?
When the value isn’t there, binarySearch returns -(insertion point) - 1, where the “insertion point” is the position where it should be to keep the order. Searching for 35 in [10, 20, 30, 40, ...], it would go at position 3 (between 30 and 40): -3 - 1 = -4. The practical takeaway: if the result is negative, it isn’t there; if it’s 0 or more, that’s its position.
And why subtract 1? Think about searching for 5. It would have to go before the 10, at position 0, and “minus 0” is 0… which is exactly what it would return if it had found it at position 0. You couldn’t tell “it’s the first one” from “it isn’t there and would go first”. By subtracting 1, any value that isn’t there gives -1 or less, and 0 stays free for “found at position 0”.
| You search for | Is it there? | Insertion point | Returns | Why |
|---|---|---|---|---|
| 30 | Yes | (not needed) | 2 | It’s at position 2 |
| 10 | Yes | (not needed) | 0 | It’s at position 0: that’s why 0 can’t mean “not there” |
| 35 | No | 3 | -4 | It would go between 30 and 40: -3 – 1 |
| 5 | No | 0 | -1 | It would go before all of them: -0 – 1 |
Check you’ve got it: with the same sorted data, what does Arrays.binarySearch(data, 90) return?
-9. 90 isn’t there and it’s bigger than all of them, so it would have to go at the end, after the 80. The array has 8 elements (positions 0 to 7), so the insertion point is 8, the new position it would take: -8 – 1 = -9.
binarySearch only works if the array is already sorted. If you use it on an unsorted array, it doesn’t give an error, but the result means nothing. Sort first with Arrays.sort.
Another catch: Arrays.equals compares only one level. If the elements are themselves arrays (a table), use Arrays.deepEquals, just like deepToString for printing.
Questions you might be asking
Can I add an element to an array?
No: the size is fixed. What you do is create a bigger array (with Arrays.copyOf(a, a.length + 1)) and put the new value in the last slot. If you need to do this often, it’s a sign you want an ArrayList.
Why do I have to say the size when I create it?
Because Java reserves a contiguous block of memory of that size. That’s what makes accessing grades[3] so fast: it knows exactly where it is without searching.
What’s the difference between an empty array and null?
new int[0] is a real array with zero elements (its length is 0 and you can loop over it with no problem). null means the variable doesn’t point to any array: if you do .length on it, you get a NullPointerException.
Can I create an array with a variable size, like new int[n]?
Yes. The size can be any expression, such as a variable read with Scanner. What can’t change is the size after it’s been created. If n is negative, it fails with NegativeArraySizeException.
A complete example
Let’s put everything together in a small program: it calculates the highest grade and the average of a group’s grades using methods, and then the average of each group in a table. Read the comments as you go; they explain the reasoning behind each decision.
import java.util.Arrays;
public class Grades {
// Returns the highest grade. Starts from the first element and compares with the rest.
static int maximum(int[] data) {
int max = data[0]; // assume the first one is the biggest
for (int i = 1; i < data.length; i++) { // start at 1: index 0 is already covered
if (data[i] > max) {
max = data[i];
}
}
return max;
}
// Returns the average. The (double) avoids integer division: 41 / 6 would give 6, not 6.83
static double average(int[] data) {
int sum = 0;
for (int n : data) { // for-each: we only read, we don't modify
sum += n;
}
return (double) sum / data.length;
}
// This method DOES modify the original array (that's why it uses the classic for)
static void raiseByOne(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i]++;
}
}
public static void main(String[] args) {
int[] grades = {7, 5, 9, 4, 10, 6};
System.out.println("Grades: " + Arrays.toString(grades));
System.out.println("Highest: " + maximum(grades));
System.out.println("Average: " + average(grades));
raiseByOne(grades);
System.out.println("After raising: " + Arrays.toString(grades));
// Three groups with a different number of students (rows of different length)
int[][] groups = {
{7, 5, 9},
{4, 10},
{6, 8, 8, 3}
};
for (int g = 0; g < groups.length; g++) {
// groups[g] is an int[]: we can pass it straight to the average method
System.out.println("Group " + g + " (" + groups[g].length + " students): " + average(groups[g]));
}
}
}
Output:
Grades: [7, 5, 9, 4, 10, 6] Highest: 10 Average: 6.833333333333333 After raising: [8, 6, 10, 5, 11, 7] Group 0 (3 students): 7.0 Group 1 (2 students): 7.0 Group 2 (4 students): 6.25
Tracing maximum, iteration by iteration
Follow maximum slowly with grades = {7, 5, 9, 4, 10, 6}. The idea, in words: you keep the first number as “the biggest so far” and look at the others one by one; if one beats it, it becomes the new biggest.
i | data[i] | max before | data[i] > max? | max after | Why |
|---|---|---|---|---|---|
| (before the loop) | 7 | (doesn’t exist) | (not compared) | 7 | It starts from data[0]: before looking at anything else, the first one is the biggest you know |
| 1 | 5 | 7 | No | 7 | 5 doesn’t beat 7: nothing changes |
| 2 | 9 | 7 | Yes | 9 | 9 beats 7: new biggest |
| 3 | 4 | 9 | No | 9 | 4 doesn’t beat 9 |
| 4 | 10 | 9 | Yes | 10 | 10 beats 9: new biggest |
| 5 | 6 | 10 | No | 10 | Last iteration: then i becomes 6, which is no longer less than 6 |
That’s why the for starts at 1: position 0 is already stored in max before the loop, and comparing it with itself would be a wasted iteration. And max starts at data[0] and not at 0 because, with temperatures below zero, starting at 0 would give a maximum that isn’t even in the array.
See how nicely the pieces fit: average works the same with the grades array as with groups[g], because each row of the table is an array of integers. That’s what it means for a table to be “an array of arrays”.
See it with Python Tutor
References are much easier to understand by seeing them than by reading about them. Go to pythontutor.com/java.html, paste this code, and step through it with “Next”:
public class Main {
public static void main(String[] args) {
int[] grades = {7, 5, 9};
int[] alias = grades; // copy or same house?
alias[0] = 99;
int[] copy = new int[grades.length];
for (int i = 0; i < grades.length; i++) {
copy[i] = grades[i]; // real copy, element by element
}
copy[1] = 0;
System.out.println(grades[0]);
System.out.println(copy[1]);
}
}
What to look for:
- After
int[] grades = {7, 5, 9};, on the right you’ll see one array and the variablegradeswith an arrow pointing to it: that arrow is the “address”. - After
int[] alias = grades;, a second arrow appears pointing to the same array, not a new one. - When
alias[0] = 99;runs, the 7 turns into 99 in the only array that exists:grades[0]is also 99. - With
new int[grades.length], a second, independent array appears, and the loop copies the values one by one. Nowcopy[1] = 0doesn’t affectgrades.
To see what Arrays.sort and Arrays.binarySearch do inside, try the Algorithm visualizer: bubble, selection, insertion, merge sort, quicksort and binary search, comparison by comparison, with the Java version alongside. It runs in your browser, on your phone too.
Summary
- An array holds values of the same type and has a fixed size; its elements can be modified.
- It’s created with braces (
{1, 2, 3}) or withnew type[size]; if you don’t fill it in, the slots hold0,0.0,falseornulldepending on the type. - Indexes go from
0tolength - 1;lengthhas no parentheses and there are no negative indexes. - You loop with the classic
for(when you need the index or to modify) or with the for-each (read only). Theforcondition:i < array.length. - An array variable holds a reference: assigning (
b = a) doesn’t copy, and==doesn’t compare contents. UseArrays.copyOfandArrays.equals. - When you pass an array to a method, the method can modify it.
- A table is an array of arrays (
int[][]), it’s looped over with two loops, and its rows can have different lengths. - The
Arraysclass (toString,sort,binarySearch,copyOf,copyOfRange,equals,fill) saves you from reinventing the wheel.
Checklist: do you have it clear?
- Can you declare an array and create it both ways (braces and
new)? - Can you say what value each slot will have if you don’t fill it in?
- Can you explain why
grades[grades.length]fails? - Do you know why
int[] b = a;doesn’t make a copy? - Can you compare two arrays by contents and print one on screen?
- Can you loop over a table with two loops, by index and by value?
If any point left you with doubts, go back to its section before moving on. In the next article, the arrays in Java practice, we’ll put all this into practice with three complete programs.
Next step: practiceArrays in Java — practice with a shop, a matrix and the class grades (and how not to ruin the original)Did this article help you? Here is how you can support SergioLearns →



