Using two-dimensional arrays
java language provides syntax support for multidimensional arrays.
If you can put a one-dimensional array as a linear geometry in the graphics, then the two-dimensional array is equivalent to a table, like the right Excel in the same form.
For two-dimensional array of understanding, we can be seen as one-dimensional arrays array1 and as another one-dimensional array array2 exist elements. In fact, from the operating mechanism underlying array, in fact, no multidimensional arrays.
statement
Data Type [] [] variable name ;
initialization
Format 1 (dynamic initialization):
grammar
int[][] arr = new int[3][2];
Explanation
It defines the name arr two-dimensional array
Two-dimensional array has 3 one-dimensional array
Each one-dimensional array has 2 elements
Name one-dimensional array are arr [0], arr [1 ], arr [2]
A first one-dimensional array to a pin assigned indexer 78 was written: ARR [0] [1] = 78;
Format 2 (dynamic initialization):
grammar
int[][] arr = new int[3][];
Explanation
Two-dimensional array has 3 one-dimensional array.
Each one-dimensional arrays are initialized default value null ( note: different format 1 )
This can be a three-dimensional array are initialized
arr[0] = new int[3]; arr[1] = new int[1]; arr[2] = new int[2];
NOTE: int [] [] = ARR new new int [] [. 3]; // illicit
Format 3 (static initialization):
grammar
int[][] arr = new int[][]{{3,8,2},{2,7},{9,0,1,6}};
Explanation
Define a name for arr two-dimensional array, a two-dimensional array of three-dimensional arrays
Each one-dimensional array of elements also have specific initialization
A first one-dimensional array arr [0] = {3,8,2} ;
The second one-dimensional array arr [1] = {2,7} ;
The third one-dimensional array arr [2] = {9,0,1,6} ;
Length represents the third embodiment of the one-dimensional array: ARR [2] .length;
note
Note that special cases written: int [] X, y []; X is a one-dimensional array, Y is a two-dimensional array.
Java multi-dimensional arrays are not necessary to rule matrix form
A first two-dimensional array [] must have the length of the array.
use
General and nested for use with
int[][] a=new int[3][2];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.println(a[i][j]);
}
}
Pascal's Triangle
// number of rows of Pascal's Triangle
int count = 10;
// define a two-dimensional array
int[][] yangHui = new int[count][];
// to two-dimensional array assignment
for(int i = 0;i < yangHui.length;i++){
// dynamic two-dimensional array initialization
yangHui [i] = new int [i + 1];
// to the beginning and end of Fu 1
yangHui [i] [0] = yangHui [i] [i] = 1;
/*
* To the beginning and end of the non-assignment , because the value is already beginning and end of 1 , so the value of the beginning and end do not change.
* J = 1: is the second number indicates the start of each array, as a first digital value has been set
* J <yangHui [i] .length : is the last digit of each array unchanged. Because the last digit is fixed
*/
for(int j = 1;j < yangHui[i].length - 1;j++){
yangHui [i] [j] = yangHui [i -1] [j] + yangHui [i - 1] [j -1];
}
}
// convenient two-dimensional array
for (int i = 0; i < yangHui.length; i++) {
for (int j = 0; j < yangHui[i].length; j++) {
System.out.print(yangHui[i][j]+"\t");
}
System.out.println("");
}