Click to enter _ more _java thousand ask
1. How to define a two-dimensional array
In the Java language, multidimensional arrays are treated as arrays of arrays.
Understanding a one-dimensional array look here: What is an array in Java
The definition is similar to a one-dimensional array, as follows:
type arrayName[ ][ ]; type [ ][ ]arrayName;
2. How to initialize a two-dimensional array
Two-dimensional array initialization and a one-dimensional array, divided into static initialization and dynamic initialization
Static initialization
In the Java language, because the two-dimensional array is considered an array of arrays, the array space is not continuously allocated, so the size of each dimension of the two-dimensional array is not required. The initialization method is as follows:
int intArray[ ][ ]={{1,2},{2,3},{3,4,5}};
Dynamic initialization
A two-dimensional array can allocate space directly for each dimension, as follows:
arrayName = new type[arrayLength1][arrayLength2]; int a[ ][ ] = new int[2][3];
You can also start with the highest dimension, allocating space for each dimension, as follows:
newtype[arrayLength1][ ]; arrayName[0newtype[arrayLength20]; arrayName[1newtype[arrayLength21]; … arrayName[arrayLength1-1newtype[arrayLength2n];
For example:
int a[ ][ ] = new int[2][ ]; a[0] = new int[3]; a[1] = new int[5];
In particular, for arrays of two-dimensional reference types, you must first allocate a reference space for the highest dimension, and then allocate space for the lower dimension sequentially. Also, you must allocate space for each array element individually.
For example:
String s[ ][ ] = new String[2][ ]; s[0]= new String[2];//为最高维分配引用空间 s[1]= new String[2]; //为最高维分配引用空间 s[0][0]= new String("Good");// 为每个数组元素单独分配空间 s[0][1]= new String("Luck");// 为每个数组元素单独分配空间 s[1][0]= new String("to");// 为每个数组元素单独分配空间 s[1][1]= new String("You");// 为每个数组元素单独分配空间
3. How to get the elements of a two-dimensional array
For each element in a two-dimensional array, it is referenced as follows:
arrayName[index1][index2]
For example:
num[1][0];
4. How to use two-dimensional arrays:
Two-dimensional arrays in mathematics, can be considered a matrix, we look at a two matrix multiplication example:
Publicclassmatrixmultiply{ Public Static void Main(String args[]) { ??intI, J, K;intA[][] =New int[2][3];//Dynamic initialization of a two-dimensional array intB[][] = {{1,5,2,8}, {5,9,Ten, -3}, {2,7, -5, - -} };//Static initialization of a two-dimensional array intC[][] =New int[2][4];//Dynamic initialization of a two-dimensional array for(i =0; I <2; i++) for(j =0; J <3; J + +) A[i][j] = (i +1) * (j +2); for(i =0; I <2; i++) { for(j =0; J <4; J + +) {C[i][j] =0; for(k =0; K <3; k++) c[i][j] + = a[i][k] * B[k][j]; }} System. out. println ("******* a********"); for(int[] aa:a) {String rowstr =""; for(intAAA:AA) {Rowstr + + AAA +" "; } System. out. println (ROWSTR); } System. out. println ("******* b********"); for(int[] bb:b) {String rowstr =""; for(intBBB:BB) {Rowstr + = BBB +" "; } System. out. println (ROWSTR); } System. out. println ("*******matrix c********"); for(i =0; I <2; i++) {String rowstr =""; for(j =0; J <4; J + +) {rowstr + = C[i][j] +" "; } System. out. println (ROWSTR); } }}
The results of the implementation are as follows:
* a********
2 3 4
4 6 8
* b********
1 5 2 8
5 9 10-3
2 7-5-18
**matrix c***
25 65 14-65
50 130 28-130
Java thousand ask _06 data structure (017) _ What is a two-dimensional array