A two-dimensional array is a table. A table is a vertical list of horizontal lists. In Java, a two-dimensional array is an array of arrays. That is, a 2d array is a vertical array of horizontal arrays. That is, a 2D array in Java is a vertical list of horizontal lists. The 2D array has rows and columns. The length of the 2D array is the number of rows, which is the length property of the 1D column array. All the values in all the cells of the table are of the same type; this is said to be the type of the 2D array.
This article explains, what a 2D array in Java is, and how to create and access its elements. All code for this article takes place in the main() method.
Creating a Two-Dimensional Array
A 1D array of chars, without initialization and without the length indicated, is declared as follows:
ar is the name of the 1D array. A 2D array under the same conditions would be declared as follows:
arr is the name of the 2D array. The length and width of this array still have to be indicated. The following code segment illustrates this:
arr = new char[4][5];
The first statement has two pairs of square brackets: The first one is for the rows, and the second is for the columns. The second statement also has two pairs of square brackets with numbers. The first one has the number of rows, and the second has the number of columns. arr, here, is a table of 4 rows and 5 columns, where each cell has the default char value.
Replacing the Default Values with Practical Values
For the 1D array, ar of chars above, a length of 4 can be given with:
ar = new char[4];
and the default char values for this 1D array, can all be replaced with:
ar[1] = 'K';
ar[2] = 'K';
ar[3] = 'K';
For the 2D array, a similar thing happens, but with two pairs of square brackets. The first pair is for the row number, and the second pair is for the column number. Index counting for both rows and columns begins from zero. So, for the 2D array,
arr = new char[4][5];
the default char values can be replaced with:
arr[1][0] = 'K'; arr[1][1] = 'L'; arr[1][2] = 'M'; arr[1][3] = 'N'; arr[1][4] = 'O';
arr[2][0] = 'K'; arr[2][1] = 'L'; arr[2][2] = 'M'; arr[2][3] = 'N'; arr[2][4] = 'O';
arr[3][0] = 'K'; arr[3][1] = 'L'; arr[3][2] = 'M'; arr[3][3] = 'N'; arr[3][4] = 'O';
Reading the Values of a 2D Array
A nested for-loop can be used to read all the values of a two dimensional array. The following nested for-loop, reads all the values of the above 2D array, row-by-row:
The output is:
K L M N O
K L M N O
K L M N O
i is for the rows; j is for the columns. Each element is accessed with, arr[i][j].
2D Array Declaration with Initialization
A 2D array can be declared and initialized with practical values, at the same time. One way of doing this, for the above array, is:
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'}
};
Notice that the numbers for the length and width of the array have been omitted, as the initializer 2D array, has the length and width implicitly. The other way to achieve declaration and initialization, omits “new char[][]”; that is:
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'},
{'K', 'L', 'M', 'N', 'O'}
};
Length and Width for a 2D Regular Array
A declaration can have the length and width as follows:
The length (height) is 4, and the width is 5. An array at this state consists of 4 X 5 cells of default values of char. This is somewhat an array of 4 cells, where each cell has five cells. The nesting cell is not coded, making it an array of five rows. With this statement, the array length property (field) gives the number of rows, as the following code segment illustrates:
The output is 4. Since the length property returns only the number of rows, the number of columns or width has to be pre-conceived.
The following nested for-loop uses the length property, to display the values of the table:
arr.length results in the number of rows. The number of columns, 5, was pre-conceived.
Conclusion
A two-dimensional array is an array of arrays. It is a table of rows and columns. It is coded as a number of rows. Java has a special syntax to create a 2D array. All the values of the 2d array are of the same type, which is also known as the type of the array. The handling of the 2D array is characterized by consecutive pairs of square brackets. In order to code a 2d array in Java, there is no need to import the array class.