Two dimensional array in java

In recent article, we have discussed the one dimensional in java. In this article, we will discuss what is two dimensional array in java or 2d array java. In 2d array in java, we used multiple dimensions to create it, so it is also known as multidimensional array java. So let’s see how we can initialize 2d array java and also see the two dimensional array in java example program.
Here is the table content of the article will we will cover this topic.
1. Declaration of Two-dimensional array in java?
2. Construction of Two-dimensional array in java?
3. Memory Representation after Construction?

4. Initialization of One-dimensional array in java?
5. Memory Representation after Initialization?

Two dimensional array java: Two-dimensional array(2-D) is the simplest form of a multidimensional array. A two-dimensional array is the array of arrays because they store in tabular form. As like single dimensional array we must declare the two-dimensional array variable. We must specify the array name followed by two square brackets called subscript([][]).

A two-dimensional array is stored in the tabular form of the row-column matrix. The two-dimensional array uses two subscripts([][]) where the first index behaves like row and second index column. The size of the matrix depends upon the size of subscripts.

Steps:

  • Declaration  of Array
  • Construction of Array
  • Initialization of Array

Declaration of two-dimensional array Java

Like the onedimension array, in two dimensional we provide data type of array and name of array for the declaration. Data type means which type of elements we want to store in Array. In two-dimensional array we are using two subscripts for declaration of array. First subscript used for ROW and second for COLUMN.

datatype[][] arrayName;
Or 
datatype arrayName[][];
Or 
datatype [][]arrayName;  
int[][] number; 
Or
int number[][];
Or 
int [][]number;   

Here int is datatype and number is the name of the two-dimensional array. Two subscripts [][] is used to defined as a two-dimensional array.

Construction of two-dimensional array Java

Like a one-dimensional array, for the creation of a two-dimensional array, the new keyword is used with the data type of array. There are two subscripts for the size of an array, is the size of the matrix. The first subscript is used to define the number of rows in the matrix and the second one is for the number of columns. You must specify the size of the array because how many number elements you can store an array directly depends upon the size of the array.

arrayName = new DataType[size][size];

new Datatype[size][size] : It creates an array in heap memory. Because an array is an object, so it stored in heap memory. The first subscript size will define the number of row and second subscript used for the number of columns.

arrayName: Assignment operator assigns the newly created array to the reference of variable arrayName.

number =  new int[3][3];  // allocating memory to array  

Memory Representation after Construction

You can see in the example we define the size of the array. First and second subscript size is 3 which means it will create a 3X3 matrix.

  Column 1 Column 2 Column 3
Row 1 number[0][0] number[0][1] number[0][2]
Row 2 number[1][0] number[1][1] number[1][2]
Row 3 number[2][0] number[2][1] number[2][2]

The first element of array is number[0][0], second is number[0][1] and so on.

Initialization of two-dimensional array Java

You can provide value to an array at declaration time. Here is the syntax of the initialization of two-dimensional array it differs from the one-dimensional array.

dataType arrayName[][]  = { 
             {value1, value2, …valueN}, 
             {value1, value2, …valueN},
             {value1, value2, …valueN},
 };  

It’s created an array and initializes it during declaration.

int[][] number = {
			{11, 22, 33},
			{44, 55, 66},
			{77, 88, 99},
		  };

Memory Representation after Initialization

In this example here, we are initializing a two-dimensional array. This array is holding the 9 elements of type int.

  Column 1 Column 2 Column 3
Row 1 number[0][0]=11 number[0][1]=22 number[0][2]=33
Row 2 number[1][0]=44 number[1][1]=55 number[1][2]=66
Row 3 number[2][0]=77 number[2][1]=88 number[2][2]=99

Two-dimensional array is an array of array. You can see the structure of array:

Two dimensional array java

Let’s take an example of a Two-dimensional array. In which we are declaring and initializing the array.

public class ExampleOfTwoDimentionalArray
 {  
	public static void main(String[] args)
	{
		// Creation of Array with initilization
		int[][] number = {
							{1, 2, 3},
							{4, 5, 6},
							{7, 8, 9},
		                 };
		String[][] names = {
							{"Hello1", "Hello2", "Hello3"},
							{"Hello4","Hello5", "Hello6"},
							{"Hello7", "Hello8", "Hello9"},
						 };
         // You can initilize the Array in another way
		/*
		 * int[][] number = new int[] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, }; String[][]
		 * names = { {"Hello1", "Hello2", "Hello3"}, {"Hello4","Hello5", "Hello6"},
		 * {"Hello7", "Hello8", "Hello9"}, };
		 */

		
		// Accessing of Array
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				System.out.println(number[i][j]);
				System.out.println(names[i][j]);
			}
		}
		
	}  
}  

Output: 1
Hello1
2
Hello2
3
Hello3
4
Hello4
5
Hello5
6
Hello6
7
Hello7
8
Hello8
9
Hello9

Let’s take another example of a one-dimensional array. In which Firstly we are declaring Array and after that initializing the array.

public class ExampleOfOneDimentionalArray
 {  
	public static void main(String[] args)
	{
		// Creation of Array
		int[][] number;
		String[][] names;
		
		// Construction of Array
		number = new int[3][3];
		names = new String[3][3];
		
		// initilization of Array
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++) 
			{
				number[i][j] = i+j;
				names[i][j] = "Hello"+i+j;
			}
		}
		
		// Accessing of Array
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++) 
			{
				System.out.println(number[i][j]);
				System.out.println(names[i][j]);
			}
		}
	}  
}  

Output:0
Hello00
1
Hello01
2
Hello02
1
Hello10
2
Hello11
3
Hello12
2
Hello20
3
Hello21
4
Hello22

1. Quiz, Read the below code and do answer.

Select the correct one to declares a two-dimensional integer array?

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

Get the value 6 out of the following array int[][] myArray = {{2, 4, 6, 8}, {1, 3, 5, 7}};

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

How many columns we have in this array
int[][] myArray = {{2, 4, 6, 8}, {1, 2, 3, 4}};

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleOfArray
 {  
         public static void main(String args[])
         {
        	 int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};

        	 int sum = 0;
        	 int col = matrix[0].length - 2;
            for (int row = 0; row < 4; row++)
            {
               sum = sum + matrix[row][col];
            }
            System.out.println(sum);
         }         
}

 

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class ExampleOfArray
 {  
         public static void main(String args[])
         {
        	 int [][] myArray = new int [4][3];
        	 for(int row = 0; row < myArray.length; row++) 
        	 {
        	   for(int col = 0; col < myArray[0].length; col++) 
        	   {
        	      if (row < col)
        	         myArray[row][col] = 1;
        	      else if (row == col)
        	         myArray[row][col] = 2;
        	      else
        	         myArray[row][col] = 3; 
        	    } 
        	  }
         }      
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

int[][] myArray = new int[4][4];

Click on anyone to know the answer.

1 thought on “Two dimensional array in java”

  1. };
    // You can initilize the Array in another way
    /*
    * int[][] number = new int[] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, }; String[][]
    * names = { {“Hello1”, “Hello2”, “Hello3”}, {“Hello4″,”Hello5”, “Hello6”},
    * {“Hello7”, “Hello8”, “Hello9”}, };
    */

    You have mentioned is wrong.
    It should be int[][] number = new int[][] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    Reply

Leave a Comment