Tricks | array
1. About the characteristics of an array
1 in Java, there is a boundary check regardless of whether an array or container is used. A runtimeexception exception is obtained if the operation is out of bounds.
2 arrays can only hold specific types. Arrays can hold basic types, and containers cannot. Containers do not work with objects in a specific type, and they treat all objects by type object.
3 The Container class can only hold references to objects. An array can either be created to directly save the base type, or you can save the object's reference. You can use wrapper classes, such as Integer, double, to save basic data type values in a container.
4 the object array and the base type array are almost identical in use; the only difference is that the object array holds the reference, and the base type array holds the value of the base type.
2. About Array Definitions
1 The array cannot allocate space when it is defined. You can allocate space to an array only after the definition is complete.
int num[];
Num=new Int[3];
Or
int num[]=new int[3];
Attention
int [] Num=new int[]{1,2,3}; Ok
int [] Num=new int[3]{1,2,3}; Error
2 can define two-dimensional arrays in this way.
int [] num;
Or
Num=new int[3][];
Num[0]=new Int[5];
Num[1]=new Int[3];
Num[2]=new int[2];
3) A two-dimensional array assigns an initial value.
int [] [] num=new int[][]{1,2,3,4,5,5}; Errorint [] num=new int[][]{{1,2,3},{4,5,5}; Okint [] num=new int[2][]{{1,2,3},{4,5,5}; Errorint [] num={{1,2,3},{4,5,6}; Ok
|
3. About array Initialization
An array of objects is automatically initialized to null at the be made early of the creation, and an array of the original data types is automatically initialized to 0 (for numeric types), (Char) 0 (for character types) or False (for Boolean types).
4. Problems with arrays about references
int[] A1 = {1, 2, 3, 4, 5};
Int[] A2;
A2 = a1;//Here just copied a reference
Look at the following code:
public class Arrays {public static void main (string[] args) {int[] A1 = {1, 2, 3, 4, 5}; for (int i = 0; i < a1.length i++) System.out.println ("a1[" + i + "] =" + a1[i]); Int[] A2; a2 = A1; for (int i = 0; i < a2.length; i++) a2[i]++; SYSTEM.OUT.PRINTLN ("-----After the change a2------"); for (int i = 0; i < a1.length i++) System.out.println ("a1[" + i + "] =" + a1[i]); SYSTEM.OUT.PRINTLN ("-----After the change a2[0]------"); A2[0] = 333; System.out.println ("a2[0]=" + a2[0]); System.out.println ("a1[0]=" + a1[0]); SYSTEM.OUT.PRINTLN ("-----a2------"); for (int i = 0; i < a2.length i++) System.out.println ("a2[" + i + "] =" + a2[i]); }}
|
Results:
A1[0] = 1
A1[1] = 2
A1[2] = 3
A1[3] = 4
A1[4] = 5
-----After the change a2------
A1[0] = 2
A1[1] = 3
A1[2] = 4
A1[3] = 5
A1[4] = 6
-----After change a2[0]------
a2[0]=333
a1[0]=333
-----a2------
A2[0] = 333
A2[1] = 3
A2[2] = 4
A2[3] = 5
A2[4] = 6
Array A1 and A2 always manipulate the same object.
5. Related Operations on arrays
1 in Java, all arrays have a default attribute length, which is used to get the number of elements in the array.
2 copy of array: System.arraycopy ().
3 Ordering of arrays: Arrays.sort ().
4 Find an element in the sorted array: Arrays.binarysearch ().
6. About sorting operations on arrays
1 The object array is sorted, the comparable interface must be implemented.
Import Java.util.arrays;class Student implements comparable {int num; String name; Student (int num, String name) {this.num = num; THIS.name = name; Public String ToString ()//Is overriding the ToString () method so that Main:System.out.println (Ss[i]); {return "number=" + num + "," + "name=" + name; public int compareTo (Object o) {Student s = (Student) o; return num > S.num? 1: (num = = s.num 0:-1); } class Arraytest {public static void main (string[] args) {student[] ss = new student[] {new Student (1, "Zhan Gsan "), New Student (2," Lisi "), New Student (3," Wangwu ")}; Arrays.sort (ss); for (int i = 0; i < ss.length i++) {System.out.println (ss[i]); } }}
|
Results:
Number=1,name=zhangsan
Number=2,name=lisi
Number=3,name=wangwu
2 to NUM as the first keyword, name for the second keyword sorting
import java.util.Arrays; class S Tudent implements comparable {int num; String name; Student (int num, String name) {this.num = num; THIS.name = name; Public String toString () {return "number=" + num + "," + "name=" + name; public int compareTo (Object o) {Student s = (Student) o; int result = num > s.num? 1: (num = = s.num 0:-1); if (0 = result) {result = Name.compareto (s.name); return result; } class Arraytest {public static void main (string[] args) {student[] ss = new student[] {new Student (1, "Zhan Gsan "), New Student (2," Lisi "), New Student (3," Wangwu "), New Student (3," Mybole ")}; Arrays.sort (ss); for (int i = 0; i < ss.length i++) {System.out.println (ss[i]); } }}
|
Results:
Number=1,name=zhangsan
Number=2,name=lisi
Number=3,name=mybole
Number=3,name=wangwu
7. About Java.util.Arrays
1) java.util.Class Arrays ' s architecture
Java.lang.Object
|
+--java.util.arrays
2) Description
This class provides essentially static methods, BinarySearch (): Search for a specific element in an array, Equals (): Compares two arrays for equality (elements at the same position are equal), fill (): Array padding, sort (): array sorting.