The arrays we used so far were uniform series arrays, where all the array elements stay in a simple list, like in a column. This is the structure of one-dimensional array, where all elements fit in one list (life is very simple). But sometimes, you may need to divide the list in a delimited sections.
For example, if you have an array of STUDENTs, and you want to add GRADEs on part on that array, and also want to include PARENTs on that array. What do we do? Well, instead of creating second array, we can add another dimension with the existing array (life is still simple). This is the idea of a multi-dimensional array. So, we can say, multi-dimensional array is a series of arrays where each array holds its own sub-array. We talked about one-dimensional arrays in our last post, and now we will walk with two-dimensional arrays and even three-dimensional arrays. Ok friends, let’s break the wall between multi-dimensional arrays and us. First take a look at this image..
2D-Array |
Creating Two-Dimensional Array
The most common and basic multi-dimensional array is the two-dimensional array. It is very simple, a two-dimensional array is an array with only two dimensions. Now, how do we create such this simple two-dimensional array? Well, just like we created our one-dimensional array, only with extra pair of [ ]. The formula would be:
DataType [ ][ ] VariableName;Does it look familiar? Yes, pretty much same as our one-dimensional array. These pair of brackets will contain some numbers. There are various way you can create two-dimensional arrays. If you want to declare the array variable but don’t want to initialize it, use the following formula:
DataType [ ][ ] VariableName = new DataType[Number1] [Number2];Now take a look at this example:
public class twoDimensionalArray {In our declaration, the students variable contains 2 lists, and each of the two lists contains 5 elements. So, whole list is made of 10 (2 * 5 = 10) elements. First list contains 5 elements and second list contains 5 elements as well. Since we have declared the variable as a String, each of the 10 elements must be a string. We can also create a two-dimensional array with more than two lists. Let me convince you with the next example:
public static void main (String[ ] args) {
String [ ][ ] students = new String[2][5];
}
}
public class twoDimensionalArray {In this time, variable is a two-dimensional array with three lists (rows) where each lists holds 5 elements. This means, the whole array contains 15 (3 * 5 = 15) elements. We should consider one more way of creation, which is:
public static void main (String[ ] args) {
String [ ][ ] students = new String[3][5];
}
}
public class twoDimensionalArray {
public static void main (String[ ] args) {
String students [ ][ ];
}
}
Why do we do that? The answer is, we do not know the size of the lists.
Initializing a Two-Dimensional Array
So far we have created and declared two-dimensional, but we haven’t initialize them, which means all elements of the array has their default values, e.g. zero (0). When we construct an array, Java automatically initializes all its values to zero(or false for an array of Boolean). We can verify this by accessing first element of a two-dimensional array. Such as…
boolean [ ][ ] booleans = new boolean [2][2];
System.out.println("Boolean[0][0]: " + booleans[0][0];
OUTPUT:
Boolean[0][0]: false
int [ ][ ] booleans = new int [2][3];
System.out.println("Int[0][0]: " + int[0][0];
OUTPUT:
Int[0][0]: 0
There are several ways to initialize a two-dimensional array. We can initialize an array variable at the same time we declare it. To do this. We don’t include values in the square brackets, on the right side of the declaration. It will look like this:
String [ ][ ] students = new String [ ][ ] . . .;
Before the closing semi-colon, type an opening and closing curly brackets. Inside the curly brackets, include a pair of an opening and a closing curly brackets for each internal array. Then inside of these new curly brackets, include a list of values for the internal arrays. Notice…
String [ ][ ] students = new String [ ][ ] {
{"Jana", "Urba", "Rose", "Amit"}, //First List
{"Nuna", "Reymon", "Marq", "Shovon"} //Second List
}
We can also initialize a two-dimensional array using a simple for loop. Behind the scene would be, walking through every columns and rows, and putting values. Look at the example…
String[ ][ ] grades = new String[44][3]; //Suppose 44 students taking 3 exams.
for (int i = 0; i < grades.length; i++){ // Loop through each row
for (int j = 0; j < grades.length; j++){ // Loop through each columns within the current row
grades[i][j] + "A"; // Enter A for each elements
}
}
Consider Some Terms of Two-Dimensional Array
_A two-dimensional array is an array references to array. So, two-dimensional array in java is simply an array of one-dimensional array. Such as, String [ ][ ] is an array of String[ ] or “Array of Array of String”.
_Because of above reason, second dimension in array is optional in Java. We can create a two-dimensional array without specifying both dimension, like int [4][ ].
_Remember, we have to specify the first dimension to create a multi-dimensional array. So, int [4][ ] is OK, but int [ ][4 ] is not OK.
_If we use for loop to initialize multi-dimensional array, we need as many for loop as many dimension of array we have. Such that, we need two nested for loops for two-dimensional array, and three nested for loops for three-dimensional array.
_From a two-dimensional array, we can find number of rows using number.length and number of columns using number[0].length expression.
_Because of above reason, second dimension in array is optional in Java. We can create a two-dimensional array without specifying both dimension, like int [4][ ].
_Remember, we have to specify the first dimension to create a multi-dimensional array. So, int [4][ ] is OK, but int [ ][4 ] is not OK.
_If we use for loop to initialize multi-dimensional array, we need as many for loop as many dimension of array we have. Such that, we need two nested for loops for two-dimensional array, and three nested for loops for three-dimensional array.
_From a two-dimensional array, we can find number of rows using number.length and number of columns using number[0].length expression.
int [ ][ ] 2Darray = new int[3][2];
int rows = 2Darray.length;
int columns = 2Darray[0].length;
That’s all about two-dimensional array in Java. It is wonderful data structure to represent two dimensional things like Matrix and some of the tile based games. By the way, it is interesting that Java doesn't support true multi-dimensional array, instead they are showed as “Array of Array”.
Nice work!
ReplyDeleteThanks for the comment!
ReplyDelete