As we have already said, arrays are capable of storing objects also. For example, we can create an array of Strings which is a reference type variable. However, using a String as a reference type to illustrate the concept of array of objects isn't too appropriate due to the immutability of String objects. Therefore, for this purpose, we will use a class Student containing a single instance variable marks. Following is the definition of this class.
class Student {
int marks;
}
An array of objects is created just like an array of primitive type data items in the following way.
Student[] studentArray = new Student[7];
The above statement creates the array which can hold references to seven Student objects. It doesn't create the Student objects themselves. They have to be created separately using the constructor of the Student class. The studentArray contains seven memory spaces in which the address of seven Student objects may be stored. If we try to access the Student objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that studentArray[0] isn't yet pointing to a Student object.
studentArray[0].marks = 100;
In this way, we create the other Student objects also. If each of the Student objects have to be created using a different constructor, we use a statement similar to the above several times. However, in this particular case, we may use a for loop since all Student objects are created with the same default constructor.
for ( int i=0; i<studentArray.length; i++) {
studentArray[i]=new Student();
}
The above for loop creates seven Student objects and assigns their reference to the array elements. Now, a statement like the following would be valid.
studentArray[0].marks=100;
Enhanced for loops find a better application here as we not only get the Student object but also we are capable of modifying it. This is because of the fact that Student is a reference type. Therefore the variable in the header of the enhanced for loop would be storing a reference to the Student object and not a copy of the Student object which was the case when primitive type variables like int were used as array elements.
for ( Student x : studentArray ) {
x.marks = s.nextInt(); // s is a Scanner object
}
If you know how to create one dimensional array and fact that multi-dimensional arrays are just array of array in Java, then creating a 2 dimensional array is very easy. Instead of one bracket, you will use two e.g. int[][] is a two dimensional integer array. You can define a 2D array in Java as follows :int[][] multiples = new int[4][2]; // 2D integer array with 4 rows and 2 columns
String[][] cities = new String[3][3]; // 2D String array with 3 rows and 3 columns
By the way when you initially declare a two dimensional array, you must remember to specify first dimension, for example following array declaration is illegal in Java. int[][] wrong = new int[][]; // not OK, you must specify 1st dimension
int[][] right = new int[2][]; // OK
The first expression will throw "Variable must provide either dimension expressions or an array initializer" error at compile time. On the other hand second dimension is optional and even if you don't specify compiler will not complain, as shown below :String[][] myArray = new String[5][]; // OK
String[][] yourArray = new String[5][4]; // OK
Read more: http://java67.blogspot.com/2014/10/how-to-create-and-initialize-two-dimensional-array-java-example.html#ixzz474GE4v00