/* related posts with thumb nails */

Multidimensional arrays:

Multidimensional arrays can represent the data in the form of rows and columns i.e. by taking two or more indexes.

Multidimensional arrays include double dimensional and 3 dimensional arrays.

Double Dimensional Arrays: Double dimensional arrays can represent the data in the form of rows and columns i.e. by taking two indexes. Generally, these are used to work with matrices.

In java, a double dimensional array can be declared as follows:

int a[][] = new int[5][5];

Or int [][]a = new int[5][5];

Or int []a[] = new int[5][5]

Or int a[][]={ {1,2,3}, {4,5,6}, {7,8,9}};

Array initialization can be done as follows:

int a[][]={ {10,20,30}, {40,50,60}, {70,80,90} };

to access the second element of second row in the array we use a[1][1]

To print the array, we may write:

for(int i=0;i<3;i++)

{

System.out.println(“”);

for(int j=0;j<3;j++)

{

System.out.print (“\t”+a[i][j]);

}

}

Three-dimensional arrays: Three-dimensional array uses three indexes to refer to its elements. It represents a set of matrices.

Ex: int a[][][] = new int [2][2][2];

In the above expression, for the array ‘a’ 32 bytes of memory would be allocated and array ‘a’ can hold maximum of 8 integers at a time. This can be treated as two-2X2 matrices.

Array initialization can be done as follows:

int a[][][]={ {10,20,30},{40,50,60}, {70,80,90},{11,22,33} };

a[0][0][0] fetches the first element of the array

To print the above array, we may write:

for (i=0;i<2;i++)

{ System.out.println(“”);

for(j=0;j<2;j++)

{

System.out.println(“”);

for(k=0;k<3;k++)

System.out.print (“\t”+a[i][j][k]);

}

}

Related Topics:

0 comments:

Post a Comment