2D Array in Java

In Java, a one-dimensional array is a consecutive set of values of the same type. The type of the values is the type of the array. A 1D array is an object from the Object superclass. A 1d array is a list. In this article, the list for the one-dimensional array is assumed to be displayed in a vertical column. A 1d array has the property length, which returns the number of elements in the array.

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:

char[] ar;

ar is the name of the 1D array. A 2D array under the same conditions would be declared as follows:

char[][] arr;

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:

char[][] arr;

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:

char[] ar;

ar = new char[4];

and the default char values for this 1D array, can all be replaced with:

ar[0] = 'K';

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,

char[][] arr;

arr = new char[4][5];

the default char values can be replaced with:

arr[0][0] = 'K'; arr[0][1] = 'L'; arr[0][2] = 'M'; arr[0][3] = 'N'; arr[0][4] = 'O';

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:

            for (int i=0; i<4; i++) {
                for (int j=0; j<5; j++) {
System.out.print(arr[i][j]); System.out.print(' ');
                }
System.out.println();
            }

The output is:

K L M N O

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:

char[][] arr = new char[][]{
                {'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:

char[][] arr = {
                {'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:

char[][] arr = new char[4][5];

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:

char[][] arr = new char[4][5];
            int len = arr.length;
System.out.println(len);

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:

            for (int i=0; i<arr.length; i++) {
                for (int j=0; j<5; j++) {
System.out.print(arr[i][j]); System.out.print(' ');
                }
System.out.println();
            }

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.



from https://ift.tt/3zgTMCl

Post a Comment

0 Comments