An array holds several elements of the same type. It is declared by adding []
after the element type:
int[] arr;
To initialize an array, you have to use the keyword new
and specify how many elements the array contains:
arr = new int[4];
While the distinction is irrelevant in most situations, it's best to think of the array variable as holding a reference to the array:
Note that all of the elements of the array are initialized to default values: 0 for numeric types, false
for booleans, and null
for reference types (objects and arrays).
If you have a small array and know the specific values you would like it to contain, you can initialize them directly:
String[] names = new String[] {"Akiko", "Bob", "Carlos", "Danielle"};
If the declaration and initialization happen on the same line, as in the example above, you are allowed to omit new String[]
.
Array elements are numbered starting at 0. Thus, arr[0]
is the first element of arr
, arr[1]
is the second, and so on.
You can use this to access or change any element of an array. If we take our array above and set
arr[2] = 8;
then the array looks like this:
Java arrays (unlike C arrays) know how long they are. The length of arr
is arr.length
(which in this case is 4).
A structure like a matrix or a checkerboard can be represented by a multidimensional array. This is declared using multiple pairs of square brackets:
int[][] m = new int[3][4];
A multidimensional array is represented as an array of arrays:
In Java, as in most modern programming languages, the two numbers specify the number of rows and the number of columns, respectively. The lower right box in the diagram above is m[2][3]
.
- Sedgewick and Wayne, Introduction to Programming in Java, Section 1.4
- Horstmann, Core Java, Volume I: Fundamentals, 11th Edition, Section 3.10
- ⭐ Can an array have length 0?
- ⭐ What is the index of the last element of an array
arr
? - ⭐ Given
what is
int[][] m = new int[3][4];
m.length
? - ⭐ How would you declare a three-dimensional array of doubles?
- ⭐ How many elements are in the array that is the value of the expression
new boolean[2][3][4][5]
? - ⭐ Is there a good way to remember that it's rows, columns and not the other way around?
- ⭐ Why does the code below throw a NullPointerException?
String[] words = new String[8]; for (String w : words) { System.out.println(w.length()); }
- ⭐⭐ How can you determine the number of columns in a two-dimensional array
m
?
- Yes, any non-negative integer is a valid length.
arr.length - 1
. This is because indices start at 0; if there are 10 elements, they are numbered 0 through 9.- 3, which is the number of rows.
length
only follows one reference and counts the number of boxes in the array at the other end. double[][][] arr;
.- 120, which is the product of the dimensions.
- Think of a can of RC Cola.
- While
words
itself was initialized, its elements were not. They therefore have the default valuenull
. m[0].length
.