Java two-dimensional array initialization and its length, java two-dimensional array Initialization
Two-dimensional array Initialization
1. Static Initialization
Int intArray [] [] = {1, 2}, {2, 3}, {3, 4, 5 }};
In Java, because two-dimensional arrays are regarded as arrays, the array space is not continuously allocated.The size of each dimension of a two-dimensional array is not required to be the same..
2. Dynamic Initialization
1) directly allocate space for each dimension. The format is as follows:
ArrayName = new type [arrayLength1] [arrayLength1];
Int a [] [] = new int [2] [3];
2) from the highest dimension, allocate space for each dimension:
ArrayName = new type [arrayLength1] [];
ArrayName [0] = new type [arrayLength20];
ArrayName [1] = new type [arrayLength21];
ArrayName [arrayLength1-1] = new type [arrayLength2n];
3) the dynamic initialization of a two-dimensional simple data array is as follows:
Int a [] [] = new int [2] [];
A [0] = new int [3];
A [1] = new int [5];
An array of two-dimensional Composite data types must beFirst, allocate reference space for the maximum dimensionAnd then allocate space for the low dimension. And,Space must be allocated separately for each array element. For example:
String s [] [] = new String [2] [];
S [0] = new String [2]; // allocate reference space for the maximum dimension
S [1] = new String [2]; // allocate reference space for the maximum dimension
S [0] [0] = new String (Good); // allocate space for each array element
S [0] [1] = new String (Luck); // allocate space for each array element
S [1] [0] = new String (to); // allocate space for each array element
S [1] [1] = new String (You); // allocate space for each array element
When using a two-dimensional array object, pay attention to the length represented by length,
Add length (such as arr. length) directly after the array name, which refers to rows );
Add length (for example, arr [0]. length) after the specified index, which refers to the elements owned by the row, that is, the number of columns.