Java-4.8 array initialization from scratch (1)
In this section, we will discuss array initialization.
1. An array is an object.
package com.ray.ch01;public class Test {public static void main(String[] args) {int[] a = {};System.out.println(a instanceof Object);}}
Output:
True
2. Assigning an array to a variable is actually assigning the array reference to the variable.
package com.ray.ch04;import java.util.Arrays;public class Test {public static void main(String[] args) {int[] a1 = { 1, 2, 3, 4, 5 };int[] a2;a2 = a1;System.out.println(a2.equals(a1));for (int i = 0; i < a2.length; i++) {a2[i] += 1;}System.out.println(Arrays.toString(a1));}}
Output:
True
[2, 3, 4, 5, 6]
From the above results, we can see that a1 and a2 point to the same object. When a2 is changed, a1 also changes.
Arrays can be not only basic types, but also objects.
package com.ray.ch04;public class Test {public static void main(String[] args) {Integer[] a = { new Integer(1), new Integer(2) };}}
3. When the read length exceeds the array length, an exception is thrown.
Modify the code above.
Package com. ray. ch04; import java. util. arrays; public class Test {public static void main (String [] args) {int [] a1 = {1, 2, 3, 4, 5}; int [] a2; a2 = a1; System. out. println (a2.equals (a1); for (int I = 0; I <= a2.length; I ++) {// change here, there is one more = a2 [I] + = 1;} System. out. println (Arrays. toString (a1 ));}}
Output:
True
Exception in thread main java. lang. ArrayIndexOutOfBoundsException: 5
At com. ray. ch04.Test. main (Test. java: 12)
4. In addition to the explicit value assignment, we can use new to create an array, but we must clearly write the length of the array.
Package com. ray. ch04; public class Test {public static void main (String [] args) {int [] a2 = new int [5]; // The length must be clearly written }}
There may be questions about why int can also use new, because it is followed by "[]", it represents not the basic type of int, it represents an array containing the int type. The array is an object, so you can use the new
5. assign values
If it is a basic type, data is directly distributed to the array. If it is a class, the reference is placed in the array.
Basic Type:
package com.ray.ch04;import java.util.Arrays;import java.util.Random;public class Test {public static void main(String[] args) {int[] a2 = new int[5];Random random = new Random(50);for (int i = 0; i < a2.length; i++) {a2[i] = random.nextInt();}System.out.println(Arrays.toString(a2));}}
Output:
[-1160871061,-1727040520,-1657178909,-765924271,-1625295794]
Object:
package com.ray.ch04;import java.util.Arrays;public class Test {public static void main(String[] args) {Book[] a2 = new Book[5];for (int i = 0; i < a2.length; i++) {a2[i] = new Book();}System.out.println(Arrays.toString(a2));}}class Book {}
Output:
[Com. ray. ch04.Book @ 61de33, com. ray. ch04.Book @ 14318bb, com. ray. ch04.Book @ ca0b6, com. ray. ch04.Book @ 10b30a7, com. ray. ch04.Book @ 1a758cb]
Summary: This chapter briefly discusses several notes about arrays.