Array
Array: The collection of data of the same type.
Defining arrays
Mode 1 (recommended, more to indicate array type)
type[] Variable name = new type[The number of elements in the array];
Like what:
Int[] A = new int[10];
The array name, which refers to a, points to the first address of the array element.
Mode 2 (same as C language)
Type variable name [] = number of elements in the new type[array];
Such as:
int a[] = new INT[10];
Mode 3 Direct initialization when defined
type[] Variable name = new type[]{comma-separated initialization value};
Where the red part can be omitted, so there are two kinds:
Int[] A = {1,2,3,4};
Int[] A = new int[]{1,2,3,4};
where int[] a = new int[]{1,2,3,4}, the second square bracket cannot be added to the array length, because the number of elements is determined by the contents of the following curly braces.
Array Usage Basics
Array length
Each array in Java has a property named length, which represents the size of the array.
The length property is public final int, that is, length is read-only. Once the array length is determined, the size cannot be changed.
Equals ()
Can a comparison of array contents use the Equals () method?
The following programs:
1 Public classarraytest2 {3 Public Static voidMain (string[] args)4 {5 int[] A = {1, 2, 3};6 int[] B = {1, 2, 3};7 8 System.out.println (a.equals (b));9 } Ten}
The result of the output is false.
Therefore, it is not possible to compare the contents of an array directly with the Equals () method, because there is no implementation in the Override object, so it is still implemented by using = = to implement the Equals () method and to compare whether it is the same object.
How do you compare it? One solution is to write your own code, and the other is to use java.util.Arrays.
The methods in Java.util.Arrays are all static. These include the various overloaded versions of the Equals () method.
The code is as follows:
1 Arrayequalstest.java2 3 Importjava.util.Arrays;4 Public classarrayequalstest5 {6 //Compare the contents of both int arrays7 Public Static BooleanIsequals (int[] A,int[] b)8 {9 if(A = =NULL|| b = =NULL )Ten { One return false; A } - if(A.length! =b.length) - { the return false; - } - for(inti = 0; i < a.length; ++i) - { + if(A[i]! =B[i]) - { + return false; A } at } - return true; - } - - Public Static voidMain (string[] args) - { in int[] A = {1, 2, 3}; - int[] B = {1, 2, 3}; to + System.out.println (Isequals (A, b)); - System.out.println (Arrays.equals (A, b)); the } *}
When an array element is not a base data type
When an array element is not a primitive native data type, it holds the reference type, not the object itself. When the object is generated, the reference points to the object, otherwise the reference is null.
such as the following procedures:
1 Arraytest2.java2 3 Public classArrayTest24 {5 Public Static voidMain (string[] args)6 {7Person[] p =NewPerson[3];8 9 //The reference type is empty when no objects are generatedTenSystem.out.println (p[0]); One A - //after the object is generated, the reference points to the object -P[0] =NewPerson (10); theP[1] =NewPerson (20); -P[2] =NewPerson (30); - - for(inti = 0; i < p.length; i++) + { - System.out.println (p[i].age); + } A at } - } - class Person - { - intAge ; - PublicPerson (intAge ) in { - This. Age =Age ; to + } -}
Output:
Null
10
20
30
You can also write directly in the initialization list:
Person[] p = new person[]{new person (ten), new person (+), new Person (30)};
Two-dimensional arrays
A two-dimensional array is an array of arrays.
Two-dimensional array basics
There are two types of basic definitions, such as:
type[][] i = new type[2][3]; (recommended)
Type i[][] = new TYPE[2][3];
The following programs:
1 Public classArrayTest32 {3 Public Static voidMain (string[] args)4 {5 6 int[] i =New int[2] [3];7 8System.out.println ("Is I an Object?")9+ (iinstanceofObject));Ten OneSystem.out.println ("is i[0] an int[]?" A+ (I[0]instanceof int[])); - - } the}
The output result is two true.
Two-dimensional array with variable length
Each element of a two-dimensional array is a one-dimensional array, and these arrays are not necessarily equal in length.
When declaring a two-dimensional array, you can specify only the first-dimension size, the second-dimension size, and then the array of different lengths. Note, however, that the first-dimension size cannot be empty (you cannot specify only the number of columns without specifying a row).
The following programs:
1 Public classArrayTest42 {3 Public Static voidMain (string[] args)4 {5 //Two-dimensional variable-length arrays6 int[] A =New int[3][];7A[0] =New int[2];8A[1] =New int[3];9A[2] =New int[1];Ten One //Error: Cannot empty the first dimension size A //int[][] b = new int[][3]; - } -}
Two-dimensional arrays can also be initialized at the time of definition, using the nesting of curly braces, which does not specify the size of two dimensions, and depending on the number of initialized values, array elements of different lengths can be generated.
The following programs:
1 Public classArrayTest52 {3 Public Static voidMain (string[] args)4 {5 6 int[] C =New int[][]{{1, 2, 3},{4},{5, 6, 7, 8}};7 8 for(inti = 0; i < c.length; ++i)9 {Ten for(intj = 0; J < C[i].length; ++j) One { ASystem.out.print (c[i][j]+ ""); - } - the System.out.println (); - } - - } +}
Output:
1 2 3
4
5 6 7 8
Java Array Basics