Multi- Dimensional Array:
Declaration:
Type vari_name[][];
The type specifies the base
type of array element as in the case of one-dimensional array. For example:
The following array used to store integers element:
in tab[][]; or int[][]ab;
or
int twod[ ] [ ] = new int [4] [5];
This allocates a 4 by 5
array and assign it to twod. Internally this matrix is implemented as
"Array of Array" of int type.
Memory Allocation:
Var=new type[rows][columns];
The variable var is the name
of two-dimensional array. The type refers to the type of the elements that can
be stored in array. The first index indicates the number of rows and the second
index indicates the number of columns in the two-dimensional array to be
allocated dynamically.
PROGRAM
Class TwoDArray
{
public static void main(String args[ ])
{
int twod [ ] [ ] = new int [4] [5];
int i,j,k=0;
for(i=0;i<=4;i++)
for(j=0;j<=5;j++)
{
twod[i][j]=k;
k++;
}
for(i=0;i<=4;i++)
{
for(j=0;j<=5;j++)
System.out.println(twod[i][j]) +
" ";
}
}
}
Comments
Post a Comment