Initialization of two-dimensional arrays
1. Static initialization
int intarray[] []={{1,2},{2,3},{3,4,5}};
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 .
2. Dynamic initialization
1) Allocate space directly for each dimension, in the following format:
Arrayname = new Type[arraylength1][arraylength2];
int a[] [] = new INT[2][3];
2) Allocate space for each dimension starting from the highest dimension:
Arrayname = new type[arraylength1][];
Arrayname[0] = new TYPE[ARRAYLENGTH20];
ARRAYNAME[1] = new TYPE[ARRAYLENGTH21];
Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N];
3) Dynamic initialization of two-dimensional arrays of simple data types is as follows:
int a[] [] = new int[2][];
A[0] = new INT[3];
A[1] = new INT[5];
For arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension , and then allocate the space in 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];//Allocating reference space for the highest dimension
s[1]= New string[2]; Allocating reference space for the highest dimension
s[0][0]= new String (good);//Allocate space separately for each array element
s[0][1]= new String (Luck);//Allocate space separately for each array element
s[1][0]= new String (to);//Allocate space separately for each array element
s[1][1]= new String (you);//Allocate space separately for each array element
When using a two-dimensional array object, note the length represented by
The array name is followed by a length (such as Arr.length), which refers to a few rows (row);
Specifies the index with length (such as Arr[0].length), which is the number of elements owned by the row, that is, the column.
The initialization of a Java two-dimensional array and the use of its length