Two Dimensional Array in Java
What is Two Dimensional array in Java ?
The array which stores the elements in rows and columns or matrix form.Here is a 2D array of 3 x 3 form. It means 3 rows and 3 columns.
[10,20,30]
[40,50,60]
[70,80,90]
Syntax to declare 2D array in Java
data_type arrayName[][] = new data_type[rowSize][colSize];
Declaration of 2D array is shown in the below given step..
int arr[ ][ ]=new int[3][3];
Example of 2D array declaration in Java:
//Program of 2D array in Java.
public class TwoDimensional {
public static void main(String[] args) {
int arr[][]=new int[2][2];
int i,j;
arr[0][0]=10;
arr[0][1]=20;
arr[1][0]=30;
arr[1][1]=40;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(arr[i][j]+" ");
}//end of inner for loop
System.out.println("");
}//end of outer for loop
}//end of main method
}//end of class TwoDimensional
Output:
30 40
In the above example, we have declared 2D array of size 2,2. Two rows and two columns. Then the values have been assigned to each location of the array. At last, the values of the array have been displayed using nested for loop.