In java, multidimensional arrays are actually arrays of arrays.
To declare a multidimensional array variable, specify additional index using another square brackets.
Example: Following declares the a two-dimensional array twoD.
int twoD[][] = new int [4][5];
This allocates a 4by 5 array and assigns it to twoD. Internally this matrix is implemented as an array of arrays of int as shown in follow.
Example:
**************************************************
public class MultiDimensionalArrayExample {
/**
* @param args
*/
public static void main(String[] args) {
/**
* Following is 4by4 two dimensional array
*/
int[][] twoD = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
{ 9, 8, 7, 6 }, { 5, 4, 3, 2 } };
/**
* By using following two for loops we can retrieve the elements of 2D
* array.
*/
for (int i = 0; i < twoD.length; i++) {// it(i)specifies the row index
for (int j = 0; j < twoD.length; j++) {// it(j) specifies the column index
System.out.println("element at row " + i + " and column " + j
+ " is " + twoD[i][j]);
}
}
}
}
OutPut:
element at row 0 and column 0 is 1
element at row 0 and column 1 is 2
element at row 0 and column 2 is 3
element at row 0 and column 3 is 4
element at row 1 and column 0 is 5
element at row 1 and column 1 is 6
element at row 1 and column 2 is 7
element at row 1 and column 3 is 8
element at row 2 and column 0 is 9
element at row 2 and column 1 is 8
element at row 2 and column 2 is 7
element at row 2 and column 3 is 6
element at row 3 and column 0 is 5
element at row 3 and column 1 is 4
element at row 3 and column 2 is 3
element at row 3 and column 3 is 2
**************************************************
To declare a multidimensional array variable, specify additional index using another square brackets.
Example: Following declares the a two-dimensional array twoD.
int twoD[][] = new int [4][5];
This allocates a 4by 5 array and assigns it to twoD. Internally this matrix is implemented as an array of arrays of int as shown in follow.
Example:
**************************************************
public class MultiDimensionalArrayExample {
/**
* @param args
*/
public static void main(String[] args) {
/**
* Following is 4by4 two dimensional array
*/
int[][] twoD = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
{ 9, 8, 7, 6 }, { 5, 4, 3, 2 } };
/**
* By using following two for loops we can retrieve the elements of 2D
* array.
*/
for (int i = 0; i < twoD.length; i++) {// it(i)specifies the row index
for (int j = 0; j < twoD.length; j++) {// it(j) specifies the column index
System.out.println("element at row " + i + " and column " + j
+ " is " + twoD[i][j]);
}
}
}
}
OutPut:
element at row 0 and column 0 is 1
element at row 0 and column 1 is 2
element at row 0 and column 2 is 3
element at row 0 and column 3 is 4
element at row 1 and column 0 is 5
element at row 1 and column 1 is 6
element at row 1 and column 2 is 7
element at row 1 and column 3 is 8
element at row 2 and column 0 is 9
element at row 2 and column 1 is 8
element at row 2 and column 2 is 7
element at row 2 and column 3 is 6
element at row 3 and column 0 is 5
element at row 3 and column 1 is 4
element at row 3 and column 2 is 3
element at row 3 and column 3 is 2
**************************************************
No comments:
Post a Comment