How is a two-dimensional array represented in java

In programming languages which are usually learnt before java(such as C, C++), a two dimensional array is represented as a vertical stack with two partitions where each partition contains a value as below :

2d-array in such cases is visualized as a vertical arrangement where columns are arranged vertically and rows are arranged horizontally. Elements in these arrays are accessed using index notation([][]) where first index is a row and second index represents the column count. Each index or position in these arrays contains a value. Example, if the above array is referenced by name sampleArray, the notation to access 7 in second row, the index will be sampleArray[1][1].

2D-Array in Java ?

A two dimensional array in java has a bit different concept. A multi-dimensional array in java is an array of arrays which means that each index of a 2d array contains a reference to another array which actually contains the elements. Thus the same 2d-array in java would be represented as :

       Illustration 2. Two-dimensional array representation in java

The above illustration depicts that each index of the main array does not contain any value but a reference to another array. In java, an array is an object(be it one-dimensional or multi-dimensional). Thus a two-dimensional array of type int is an object where each index holds a reference to an int array(int []) or in other words each index points to a one-dimensional array. The second index holds the actual int values.
In above illustration, if the name of array is sampleArray, then below statements will make the concept more clear.

int value = sampleArray[1][1];     // value will be 7
int[ ]  newArray = {2, 3};       // create a new one-dimensional array
sampleArray[1] = newArray;    // assign a one-dimensional array to 2d-array index 1
value = sampleArray[1][1];      // value will be now 3
sampleArray[1][1] = newArray;      // compiler error since index[1][1] should contain a value and not an array
int[ ] arrayFrom2dArray = sampleArray[2];     // assign the array referenced by the third location to a new array

Declaration

A 2d array in java is declared using the below syntax:

int[ ][ ] sampleArray = new int[3][2];

The above syntax instructs the JVM to create an array referenced by name sampleArray with 3 locations and each location will point to an array of 2 elements. Note that it is a good practice to provide both the dimensions while declaring an array but only the first dimension is necessary. Above array can also be declared as :

int[ ][ ] sampleArray = new int[3][ ];

If we observe the array shown in illustration 2, it should have been declared as

int[ ][ ] sampleArray = new int[5][2]; or simply
int[ ][ ] sampleArray = new int[5][ ];

Initialization

There are 2 ways to initialize elements of a 2d- array in java.

1. At the time of declaration
Initializing array elements at the same location where it is declared as:

int[ ][ ] sampleArray = { {1, 2}, {3, 4}, {5, 6}};

There is no need to provide the size of array if it is initialized this way since the size is automatically calculated from the number of elements. Above array contains 3 elements with each index pointing to an array of 2 elements.

2. Initializing individual elements after declaration
In this method, array is declared normally and its elements can be initialized later on as :

int[ ][ ] sampleArray = new sampleArray[3][2];    //array of 3 elements with each index pointing to an array of 2 elements
sampleArray[0][0] = 1;      // first element of the array pointed by the first index of main array
sampleArray[0][1] = 2;      // second element of the array pointed by the first index of main array

and so on…

Let’s tweak in :

  1. Though the method to access individual elements in both conventional and java arrays is same, conceptually the elements are arranged differently.
  2. A 2d array in java does not require both the dimensions at the time of declaration, only the first dimension which represents the number of rows is necessary. Thus, the declaration int[][] array  =new int[4][]; is valid.
  3. Array values are initialized automatically according to the type of the array in java. That is, all elements of an array of type int will be initialized to 0, elements of boolean type will be false and so on.
  4. Since arrays in java are objects they are created using new keyword.

 

Mark Your Impression

Close Menu