In this section, let's look at the array in a comprehensive way.
1. The array before initialization. We can't use his references to do whatever it is.
Package Com.ray.ch14;public class Test {public static void main (string[] args) {int[] a;//System.out.println (a);//error, The local variable a May is not having been//initialized//a[0]=0;//error,the local variable A May is not having been initialized}}
The above error message already illustrates the point.
2. When the array is initialized, it is assumed to be a numeric type within the underlying type, then initialize each element to 0, assuming it is char. is ' null ', assuming it is a Boolean. is false, assuming that it is a different type (contains an underlying type that is not a numeric type). The initialization of each element is null
Package Com.ray.ch14;import Java.util.arrays;public class Test {public static void main (string[] args) {int[] a = new int[ 3]; String[] B = new string[3];char[] c = new char[3];boolean[] D = new boolean[3]; myclass[] myclasses = new Myclass[3]; System.out.println (Arrays.tostring (a)); System.out.println (arrays.tostring (b)); System.out.println (Arrays.tostring (c)); System.out.println (arrays.tostring (d)); System.out.println (arrays.tostring (myclasses));}} Class MyClass {}
Output:
[0, 0, 0]
[NULL, NULL, NULL]
[ , ,]
[False, False, false]
[NULL, NULL, NULL]
3. Two ways to create an array. Explicit new vs. implicit new
Package Com.ray.ch14;public class Test {public static void main (string[] args) {myclass[] a = new myclass[3]; Myclass[] B = {new MyClass (), New MyClass (), New MyClass ()};}} Class MyClass {}
The above two ways of creating are equivalent.
4. The above implicit new is actually the aggregation initialization. But. He has a certain limitation, that is, it must be initialized at the current location. Let's look at the following. There is also a dynamic aggregation initialization
Package Com.ray.ch14;public class Test {public static void main (string[] args) {myclass[] a = new myclass[] {new MyClass ( ), New MyClass (), New MyClass ()}; Myclass[] B = {new MyClass (), New MyClass (), New MyClass ()};}} Class MyClass {}
Observe the above code. Array A is more flexible.
5. The array identifier is a reference. Point to the object inside the heap.
Package Com.ray.ch14;public class Test {public static void main (string[] args) {int[] ints = new int[] {1, 2, 3}; System.out.println (INTs);}}
Output:
[[Email protected]
The above output is actually a memory address.
6. The underlying type array stores the values of the underlying type. Object arrays are stored as references to objects (memory addresses)
Package Com.ray.ch14;public class Test {public static void main (string[] args) {int[] ints = new int[] {1, 2, 3};for (in t i = 0; i < ints.length; i++) {System.out.println (ints[i]);} myclass[] myclasses = new myclass[] {new MyClass (), New MyClass (), New MyClass ()};for (int i = 0; i < myclasses.length ; i++) {System.out.println (myclasses[i]);}} Class MyClass {}
Output:
1
2
3
[Email protected]
[Email protected]
[Email protected]
Summary: The above summarizes several aspects of the array. As well as various aspects of the examples that have been made.
This chapter is here. Thank you.
-----------------------------------
Folder
Get to know java-14.2 from scratch learn more about arrays