Java record-31-Java Array
1. Array: a set of data of the same type is called an Array. 2. how to define an array: type [] variable name = new type [number of elements in the array]; an integer array with a length of 10 can be defined as follows: int [] a = new int [10]; 3. the element index in the array starts from 0. For the array, the maximum Array Index = the length of the array-1.4. array definition and assignment:
Int [] a = new int [2]; a [0] = 1; a [1] = 2; int a [] = new int [2]; // keep up with the following definition method: int [] a = {1, 2}; // assign the initial value when defining. Int [] a = new int [] {1, 2}; // assign the initial value int [] a = new int [2] {1, 2} when defining }; // This definition is incorrect. Compilation exception: // Cannot define dimension expressions when an array initializer is provided
5. array loop assigned initial values:
int[] a = new int[100];for(int i=0; i<a.length; i++){ a[i] = i+1;}
In Java, each array has an attribute named length, indicating the length of the array. A. length = 200; // compilation error. The value cannot be specified for the final variable length. The length attributes are public, final, and int. Once the length of the array is determined, the size cannot be changed. 6.
int[] a = new int[3];System.out.println(a[2]);
When defining an array, each element is assigned an initial value by default, and an integer value is assigned 0.7. the types of each element in the array are the same, and the size and length of the occupied address space are the same. The underlying layer is placed in order, and the search is performed in order. 8.
int[] a = {1,2,3};int[] b = {1,2,3};System.out.println(a.equals(b));
Arrays are also inherited from the Object class, And the equals method of the Object is not overwritten. The equals method cannot be used to compare whether the content of the two arrays is the same. 9. int [] a = {1, 2, 3}; a is an array variable reference that points to the first address of the array, that is, the address of the first element. Each element type in the array is int type, where only the data value itself is stored. 10. The array not only stores the native data type, but also stores the reference type.
public class ReferenceArray { public static void main(String[] args) { Person[] p = new Person[2]; p[0] = new Person(20); p[1] = new Person(30); for(int i=0; i<p.length; i++){ System.out.println(p[i].age); } }}class Person{ public int age; public Person(int age){ this.age = age; }}
Person is a custom class. You can also create an array and the default initial value is null. 11. Person [] p = new Person [10]; after the code above is executed, how many Person objects will be generated? The answer is none. The Code only generates an array of the Person type. The default values are null and the Person object is not generated. Each element in the array is a reference rather than an object. It is the address of the Person object. Different from the native data type array, the native data type array will store the corresponding default value by default, for example, int will store 0. 12. elements in the array: each element in the native data type array stores the data value itself. Each element in the referenced array contains the reference address of the object, not the object itself. 13. Two-dimensional array: a two-dimensional array is a plane structure, which is essentially an array. Two-dimensional array definition method: type [] [] aa = new type [2] [3]; two-dimensional array can be an irregular array. 14.
public static void main(String[] args) { int[][] aa = new int[2][3]; System.out.println(aa[0] instanceof int[]); aa[0][0] = 1; int[][] bb = new int[2][]; int[][] cc = new int[][]{{1,2,3},{2}}; }
Two-dimensional array traversal generally uses two layers of nested loops, while one-dimensional arrays generally use one layer of cyclic traversal. Next article: http://www.bkjia.com/kf/201510/447458.html