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:
public class arraytest{public static void Main (string[] args) { int[] a = {1, 2, 3}; Int[] B = {1, 2, 3}; System.out.println (A.equals (b)); } }
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:
Import Java.util.arrays;public class arrayequalstest{ //compare the contents of both int Arrays public static bool Ean Isequals (int[] A, int[] b) { if (a = = NULL | | b = = NULL) { return false; } if (a.length! = b.length) { return false; } for (int i = 0; i < a.length; ++i) { if (a[i]! = B[i]) { return false; } } return true; } public static void Main (string[] args) { int[] a = {1, 2, 3}; Int[] B = {1, 2, 3}; System.out.println (Isequals (A, b)); System.out.println (Arrays.equals (b));} }
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:
public class arraytest2{public static void Main (string[] args) { person[] p = new Person[3]; When no objects are generated, the reference type is null System.out.println (p[0]); After the object is generated, the reference points to the object p[0] = new Person (ten); P[1] = new Person (a); P[2] = new person (in); for (int i = 0; i < p.length; i++) { System.out.println (p[i].age);}} } Class person{ int age; Public person (int.) { this.age = age; }}
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:
public class arraytest3{public static void Main (string[] args) { int[][] i = new int[2][3]; System.out.println ("Is I an Object?" ) + (i instanceof Object)); System.out.println ("is i[0] an int[]?" + (I[0] instanceof int[]));} }
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:
public class arraytest4{public static void Main (string[] args) { //two-dimensional variable-length array int[][] A = new int[3][]; A[0] = new int[2]; A[1] = new Int[3]; A[2] = new int[1]; Error: Cannot empty the first dimension //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:
public class arraytest5{public static void Main (string[] args) { int[][] c = new Int[][]{{1, 2, 3},{4},{5, 6, 7, 8}}; for (int i = 0, i < c.length; ++i) {for (int j = 0; j < c[i].length; ++j) { System.out.print (c[i][j]+ " "); } System.out.println ();}}}
Output:
1 2 3
4
5 6 7 8
Java SE Basics Review-arrays