Arrays
An array stores several values under one name
When you need to store not one value but a whole sequence of values of the same type, creating a separate variable for every element quickly becomes inconvenient. An array groups those values into one structure and lets you access each element by its number — its index.
int[] temperatures = new int[4];
temperatures[0] = 18;
temperatures[1] = 21;
temperatures[2] = 20;
temperatures[3] = 23;
The notation int[] means “an array of int values,”
and new int[4] creates an array with four elements.
The array size is set when the array is created and does not change afterward.
Indexes start at zero
The first array element has index 0, the second has index 1, and so on.
So an array with four elements has valid indexes from 0 through 3.
You can use an index both to read a value and to replace it.
int[] temperatures = {18, 21, 20, 23};
int first = temperatures[0];
temperatures[2] = 24;
System.out.println(first); // 18
System.out.println(temperatures[2]); // 24
If you access an index that does not exist, such as temperatures[4],
Java ends that operation with an ArrayIndexOutOfBoundsException.
That is why it is important to account for the actual array length when working with indexes.
Creating an array with ready values
If the values are known in advance, you can create an array more concisely by listing its elements inside curly braces. Java determines the size automatically from the number of values.
int[] scores = {10, 15, 20, 25};
boolean[] answers = {true, false, true};
If you create an array by specifying only its size, its elements receive default values.
For example, elements of an int[] start as 0,
while elements of a boolean[] start as false.
Array length and iterating over elements
The number of elements is stored in length.
This is convenient in a loop because the code does not depend on how many elements a particular array contains.
int[] scores = {10, 15, 20, 25};
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
length is not a method.
Write scores.length, without parentheses.
If you do not need the index and only want to visit every value,
you can use a for-each loop.
for (int score : scores) {
System.out.println(score);
}
A regular for loop is more convenient when you need an element's index or need to change array elements.
A for-each loop is simpler when you only need to read every value in sequence.
An array can be passed to a method
An array is used like a regular method parameter: the parameter type simply includes square brackets. This lets you write one method that can process arrays of different sizes.
static int sum(int[] numbers) {
int result = 0;
for (int number : numbers) {
result += number;
}
return result;
}
Calling sum(new int[]{2, 4, 6}) returns 12.
The method does not need to know in advance how many elements it will receive: it gets the length from the array itself.
More than one dimension
Array elements can themselves be arrays. This can represent, for example, a table where the first index selects a row and the second selects an element inside that row.
int[][] table = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(table[1][2]); // 6
Here, table[1] is the second inner array,
and table[1][2] is its third element.
For now, it is enough to think of a multidimensional array as an array that contains other arrays.
new int[4], you cannot add a fifth element to it.
When you need a structure whose size can conveniently change while the program is running, we will introduce collections.