Multi-dimensional Array

In this section, we will continue on Multi-dimensional Array from Array Arrays in Java from where we left in last section. As we know, Array in java is the basic foundation block which help programmer to write complex application easily.

Objective:

  • Memory Allocation
  • Array creation without new keyword:
  • Create a multi dimensions Array

Now we are clear on the concept of the creation of an Array and steps to create and array.

But after creation of an Array where it will be stored? We know that new Keyword allocates the memory for an array. Let’s understand the memory allocation of an Array in detail.

Memory Allocation:

As we know new keyword will help to allocate memory for an Array, but form which location it will allocate memory?

new” keyword will allocate the memory from the heap location. If [] is associated with the variable, then it is a reference variable. Therefore arr in our earlier example is a reference variable. We know all reference variable holds the address of the memory location. So arr holds the address of the memory location of the array.

The “new” keyword will allocate the memory from heap location according to the size of an array. Let’s see with a graphical representation.

Array in Java

From the above graphical representation, you can notice the address of the array is store in stack and the value of the element is stored in heap location. The Address of the first element is the address of the memory location for an array which is stored in stack and map the address in the heap location. So our reference variable always created inside the stack. Here the starting address of the memory location is 65550 and the same address is stored in the reference variable arr. As integer takes 4 bytes of memory the address of the next memory location is 65550 + 4 b= 65554 and so on. This continuous memory allocation us done for an array in the heap location.

System.out.println(arr[0]) 
Output: 90
System.out.println(arr[1])
Output: 12
System.out.println(arr[2])
Output: 34

If you want to access the element of an array from the heap location, then you need to use the index value with the reference variable as below.

Note: Arrays are actually an Objects. We can execute methods on the array. But we cannot create subclass or child class of arrays.

Create an Array without new keyword:

In the previous section, we have understood that new keyword allocates the memory for the array from the heap location. But can we create an Array in Java without new keyword?

Let’s understand this with an example:

Example:

class arrayDemo{
	public static void main(String args[]){
		int[] arr = {90,12,65};
		System.out.println("Values : "+ arr[0]);
		System.out.println("Values : "+arr[1]);
		System.out.println("Values : "+arr[2]);
 	}	
}

Output:

Values : 90
Values : 12
Values : 65

Explanation:

You can see the above example that, we have not used any new keyword but it allocates the memory in heap location. While we are trying to print the value of the array element then we are getting the value in the output. In this situation while we directly initialize the values to an array, java compiler implicitly calls the new operator to allocate the memory from heap location.

What will happen if you will access the array element beyond the size?

Example:

class arrayDemo{
	public static void main(String args[]){
		int[] arr = {90,12,65};
		System.out.println("Values : "+ arr[0]);
		System.out.println("Values : "+arr[1]);
		System.out.println("Values : "+arr[2]);
		System.out.println("Values : "+arr[3]);
 	}	
} 

Output:

Explanation:

If you will access the array element beyond the size limit, then it will throws and compile time error as shown in above example.

Multi-dimensional Array:

From the name, you can guest what is multidimensional array is about.

Yes, that is correct. Multidimensional array is an Array in Java which has array inside array. It can be two-dimensional array, three-dimensional array etc.

Let’s discuss about two-dimensional array with a simple example.

Two-Dimensional Array:

This is actually one level up of the one-dimensional array. One dimensional array is nothing but whatever we have discussed in last section i.e. simple array.

Now to define two-dimensional array we need row and column. As below

[0][0][0][1][0][2][0][3]
[1][0][1][1][1][2][1][3]
[2][0][2][1][2][2][2][3]

From the above structure you can able to know that we are having 2 index for each element in two-dimensional array. The index of first element is [0][0], second element is [0][1] and so on.

Each [ ] is showing an array. That means in two-dimensional array we are having 2 array which is denoted as array inside an array.

First [ ] signifies the rows of the two-dimensional array and second [ ] signifies the columns of the two-dimensional array.

To declare the two dimensional, we need to specify the rows and column details like below.

Syntax:

Int arr[ ][ ] = new int[3][4]

In the above syntax, you can see that we have declared the array with two [ ], one for rows and other for columns. In first [ ]  we have mentioned 3 which specifies as number of rows and 4 for second [ ] which specifies number of column.

So in simple word you can consider the above array as two-dimensional array with 3 rows and 4 columns.

Now we are done with the declaration of two-dimensional array. But how to access it?

To access the two-dimensional array is same as the simple one-dimensional array as below.

If you want to access first row first column element, then you need to access with the index [0][0]. Same as for second column second row, you need to use [1][1] as index and so on.

Now Let’s understand the memory allocation for the two-dimensional array.

The above graphical representation specifies the memory allocation for the two-dimensional array. As we know from, array reference is stored in stack memory location and the array element is stored inside the heap.

Let’s understand how to declare and access the two-dimensional array with a simple example.

Example:

class MultiDimeDemo{
	public static void main(String args[]){
		int arr[][] = new int[3][4];
		System.out.println(arr[0][0]);
		System.out.println(arr[2][2]);
	}	
}

Output:

0
0

Explanation:

In the above example of Multi-dimensional Array(2 dimensional), we trying to print the first row first column element and third row third column of the array named arr.

As we have not assigned/ initialized any values to the array element, the default value is assigned to the array which is 0 in our case for int data type. That’s why we are seeing 0 in output.

What will be the output if you want to print only the row index? Let’s have a look on this.

Example:

class Try{
	public static void main(String args[]){
		int arr[][] = new int[3][4];
		System.out.println(arr[0]);
		System.out.println(arr[2][2]);
	}	
}

Output:

[I@15db9742
0

Explanation:

As we have seeing in the graphical representation of the memory allocation of the Multi-dimensional Array (two-dimensional array), the first index (row index) is storying the address of the inner array. So, if we will try to print the arr[0] which is the first row, it will print the address of first row array. Here in our case  “[I@15db9742” is address of first row array.

You also can create a two-dimensional array with unequal dimension like in first row, there is an array of one element, in second row, there is array of 2 element. And in third row, there is an array of 3 element.

Let’s discuss this with an example.

Example:

class Try{
	public static void main(String args[]){
		int arr[][] = new int[3][];
		arr[0] = new int[1];
		arr[1] = new int[2];
		arr[2] = new int[3];
		
		int i,j,k=0;
		
		for(i=0; i<3; i++){
			for (j=0; j<=i; j++ ){
				arr[i][j] = k;
				k++;
			}
		}
		for (i=0; i<3; i++){
			for (j=0; j<=i; j++ ){
				
				System.out.print(arr[i][j]+ " ");
			}
			System.out.println();
			}
		}
}

Output:

0
1 2
3 4 5

Explanation:

In the above example, if can create an array reference with only one row index and assig different size of array to each of array refence as we declared in example. For design this to a pyramid, we need  2 for loop. One is for row and other is for column. Based on required pyramid we can configure for loop.

We will discuss more on Pyramid structure later.

Summary:

  • Multi-dimensional Array is nothing but having array element inside the array in row-column manner.
  • new” keyword will allocate the memory from the heap location.
  • All reference variable holds the address of the memory location.
  • new will allocate the memory from heap location according to the size of an array.
  • You can also create an Array in Java without new Keyword.

Leave a Reply

Your email address will not be published. Required fields are marked *