Basic skills for Java Programmers-array and memory control
A Java array is a set of values of the same type and a reference type in Java. To use an array in Java, the array must be initialized. Once the array is initialized, its length cannot be changed, and each value in the array is assigned an initial value.
Java array initialization includes static initialization and dynamic initialization.
Static initialization: the initial values of elements in the specified array displayed by the programmer. The length of the array is determined by the system.
int[] arr = new int[]{0,1,2};
Dynamic initialization: the length of the specified array displayed by the programmer. The system assigns the initial value to each element in the array.
int[] arr = new int[3];
The Java array is a static array and its length cannot be changed. Unlike the Javascript dynamic language, its array length can be dynamically changed.
<script type="text/javascript">var arr = [];arr[6] = 10;alert(arr.length);</script>
Is initialization required before using Java arrays?
// Static initialization int [] arr1 = new int [] {0, 1, 2}; // initialization of int [] arr2; arr2 = arr1; For (INT pre: arr2) {system. out. println ("pre:" + pre );}
We can see that arr2 is not initialized, but can still be used. Java array is of reference type
Variable, which does not represent the array object itself, as long as it points to a valid array object, pointing to a valid memory, the array variable can be used in the program.
Each element in the Java array is a variable, and Java does not have a multi-dimensional array. A multi-dimensional array indicates that each element in the first one dimension of the array is a reference variable and references an array of the same type.
int[][] arr =new int[3][];arr[0] = new int[]{0,1};arr[1] = new int[]{0,2};arr[2] = new int[]{0,3};System.out.println(arr.length);System.out.println("----------------");for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr[i].length; j++) {System.out.println(arr[i][j]);}System.out.println("----------------");}