JAVA Chapter 4 array, java Chapter 4 Array
ArrayA group of ordered data with the same type is saved.
1. Create:
Array declarationFormat: Int arrary [];
Int [] array1, array2; // declare multiple arrays at the same time.
The preceding statement only declares the array and does not allocate memory to it. It cannot be stored or accessed. Arrays in Java can be seen as special objects, and memory space can be allocated to Arrays Using new.
ArrayInitialization:Int array [] = new int [5]; // method 1. The default value is displayed!
ArrayAnother Creation Method: Int array [] = {1, 2, 3, 4, 5 };
2. ArrayCopy:
Int array1 [] = {1, 2, 3 };
Int array2 [] = {4, 5, 6 };
Array1 = array2; // The rough method is to point array1 to the memory space of array2, and the contents in array2 are not copied to array1. Here, only values are assigned.
System. arraycopy (fromArray, fromIndex, toArray, toIndex, length); // The method for enabling array copy.
3,Multi-dimensional array:
Create: int [] [] array2D = new int [3] [3];
Int [] [] array2D ={{ 1, 1, 1} {2, 2, 2} {3, 3, 3 }};
4,Irregular Array:
Create: int [] [] array2D = new int [3] []; // The array row must be determined when it is declared. The number of rows can be determined again!
Array2D [1] = new int [1];
Array2D [2] = new int [2];
Array2D [3] = new int [3];
5. For-Each statement:
The for loop can only take effect For one array. To obtain Each element of a multi-dimensional array, use the for-Each loop statement.
FormatIs:For (data type variable: Set)The/* for keyword is followed by the Data Type of the Set, followed by an element used for operations, which indicates the currently accessed collection element, and finally the set to be accessed. */
Statement Block
For example:Int nums [] [] = {, 3}, {, 6}, {, 9 }};
For (int x []: nums)
For (int y: x ){
System. out. print (y + "");
}