<Thinking in Java> Reading Notes -Array An array is only of the same type. It is an object sequence or a basic data type sequence encapsulated by a flag name.
Array InitializationIn Java, the format of an array can be defined in the following two methods: int [] A1; int A []; the two methods have the same results. The above only defines the reference of an array, in this case, the compiler does not allow you to specify the size of the array, because all you have is a reference to the array, and no space is allocated for the array. The following method is incorrectly defined: int [5] A; int A [5]; if an array of the basic data type is generated, by default, Java sets each element in the array to 0 (false if it is Boolean). If an object array is declared, the following statement is used: object [] OBJ = new object [10]; Initialization is not completed. Each element of OBJ must be assigned a value. The initialization process is complete.
Array ReplicationIn Java, you can assign an array value to another array value, so you can write the following statement: int [] a1 = new int [5];
Int [] A2;
A2 = A1; in fact, what we do is to copy a reference so that A2 points to the first address of A1. If the element value in A1 changes, the corresponding element in A2 also changes. Use the following example for verification: int [] a1 = new int [5];
For (INT I = 0; I <a1.length; I ++)
A1 [I] = I;
Int [] A2;
A2 = A1;
For (INT I = 0; I <a2.length; I ++)
A2 [I] ++;
For (INT I = 0; I <a1.length; I ++)
System. out. println ("A1 [" + I + "] =" + A1 [I]); the output result is: 1, 2, 3, 4, 5, and the elements in A2 change, the elements in A1. If you want to make A2 unrelated to A1, that is, after the A1 value is assigned to A2, the elements in A1 are changed, and the corresponding elements in A2 are not affected, you can use the following method: int [] a1 = new int [5];
For (INT I = 0; I <a1.length; I ++)
A1 [I] = I;
Int [] a2 = new int [5];
System. arraycopy (A1, 0, A2, 0, a1.length );
For (INT I = 0; I <a2.length; I ++)
A2 [I] ++;
For (INT I = 0; I <a1.length; I ++)
System. out. println ("A1 [" + I + "] =" + A1 [I]); at this time, the output result is:, 4 in sequence, if system. arraycopy () method, A2 must be initialized. Otherwise, a null pointer exception is reported. Using the system. arraycopy () method to copy an array is much faster than using the for () loop.
Sort arrays by comparisonYou can directly use arrays to sort arrays by comparison. the sort () method does not need to write a function by yourself. To sort a string array in the forward order, use the following program: String [] b2 =... {"B", "E", "C", "D "};
Arrays. Sort (B2 );
For (INT I = 0; I <b2.length; I ++)
System. Out. println ("B2 [" + I + "] =" + B2 [I]); To implement reverse sorting, you can use the following method:
String [] b1 =... {"B", "E", "C", "D "};
Arrays. Sort (B1, collections. reverseorder ());
For (INT I = 0; I <b1.length; I ++)
System. Out. println ("B1 [" + I + "] =" + B1 [I]);
I used to write a method to sort string arrays. For details, refer to the string operation method in Java. Obviously, this method is more troublesome.OthersIn arrays, there are two methods worth noting: Fill () is used to fill the entire array with a certain value in binarysearch () to find an element in the sorted array.