Just like one-dimensional arrays, a two-dimensional array can also be passed to a method and it can also be returned from the method. The syntax is similar to one-dimensional arrays with an exception that an additional pair of square brackets is used. Two-dimensional Array is specified by taking additional square brackets i.e. “[][]”.
Arrays are generally initialized with the new command, which creates a new instance of a reference data type. There is only one way to reference the items: by using a subscript to indicate which element number to access. The number used to reference the specific element of an array is called the component. In Java, the subscripts always start at zero and go to the length of the array minus 1. The new command initializes the array to null characters. Reference to elements in the array is by subscript or component. All the elements of an array must be of the same type.
Example:
int c[][]=new int[3][3];
The left index indicates row number and right index indicates the column number.
Here the number of rows represent the number of integer references to which “c” is pointing. The number of columns represents the length of the integer array to which each element of the array of references points.
import java.io.*;
class Matrix3x3
{
public static void main(String args[]) throws IOException
{
BufferedReader BR=new BufferedReader(new InputStreamReader (System.in));
int Number[][]=new int[3][3];
int i,j;
String m;
System.out.println("Enter Elements for Matrix 3x3 :");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
m=BR.readLine();
Number[i][j]=Integer.parseInt(m);
}
}
System.out.println("Elements in Matrix are : ");
System.out.println("");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(Number[i][j]+"\t");
}
System.out.println();
}
}
}