1.arrays are containers of basic data types and string types (reference data types), and collections are containers of class data types;2, the format of the array definition: (1) General format: element type [] Array name = new element type [element number or array length];where the element type [] Array name is a reference data type, stored in the stack; the new element type [element number or array length] is stored in the heap array object, inherited from object. (2) define and statically initialize element type [] Array name = {,,}; element type [] Array name = new element type [element number or array length]{,,,,,,,}; Note: Do not forget the end; 3. The default value of uninitialized arrays is the basic type of the Save Default initial value int[] Rfinit = new Int[3]; System.out.println (Rfinit[0]);//0 string[] strinit = new String[3]; System.out.println (Strinit[0]);//null4, Array usage FAQ (1) array out of bounds: Compile no problem, but run the times wrong. int[] arr = new INT[3]; System.out.println (Arr[3]); ArrayIndexOfBoundsException3: When manipulating an array, it accesses a corner label that does not exist in the array. Note: The maximum value of the corner mark is the length minus 1, (2) the array pointing to error string[] strnull = null; System.out.println (Strnull[0]); Nullpointexception: null pointer exception, when reference does not have any point to null, the reference is also used to manipulate the entity. 5.Array reference value: int[] a = {1,2,3,4}; System.out.println (a); Result: [[email protected], this is the reference value of the array, where [represents the arrays, I represents the storage type, and the 1db9742 hash value represents the hexadecimal address that is stored in memory. 6. Array manipulation (algorithm)traversal, max/min, sort (bubbling, selection, and direct insert), find (binary), delete array element manipulation, and conversion (practice once)(1) traverse int[] arr = new INT[3]; The length of the array can be obtained by arr.length the array's properties. (2) Max/min: A, store value (note: The variable that holds the maximum value cannot be assigned a value of 0, because there are negative numbers in the array, it can cause problems.) B, Storage Corner label (note: The value of the storage angle can be 0, because it represents the array of 0-point data) (3) Sort: From small to large A, select: a comparison within the cycle, select the smallest placed in the front, so reduce the amount of the previous comparison; (note: Temporarily save maximum or minimum value The final re-exchange is an optimized method. B, bubble: Within the cycle of a comparison, the minimum value of moving forward, the maximum value of the sink; So reduce the number of comparisons that follow,-X: Let each of the elements of the comparison decrease,-1: Avoid the corner mark out of bounds. C, direct insertion sort: Just take out an empty position, save it, then compare the element in front of this position with the value of this position, if it is greater than, an element, if it is less than, to end the comparison, save the element in its present position. Note: In Java, sort by arrays.sort (), a sort that is already defined in Java. In development, sorts the array. The sentence code to use. (4) Find: Binary find first sort, then binary compare; get the subscript value of the corresponding array.
Import java.util.*; Class arraytest2{//Select sortpublic static void Selectsort (int[] arr) {//int temp = 0; for (int i=0;i<arr.length-1;i++) {Comparison of//i subscript with J Subscriptfor (int j=i+1;j<arr.length;j++) {if (Arr[i]>arr[j]) {//First data is compared to subsequent elements (at this point, the first data is absolutely the smallest element) before the second data is compared with the subsequent datatemp = Arr[i]; Arr[i] = Arr[j]; ARR[J] = temp; Swap (ARR,I,J); } } } } //bubble sort or sort on sinkpublic static void Bubblesort (int[] arr) {//int temp = 0; for (int i =0;i<arr.length-1;i++) {//j Subscript and j+1 subscript for comparisonfor (int j=0;j<arr.length-1-i;j++) {//-1-i is because the last element has been determined to be in line, no more comparisons.if (Arr[j]>arr[j+1]) {//temp = Arr[i]; Arr[i] = Arr[j]; ARR[J] = temp; Swap (arr,j,j+1); }}}}//Optimized selection sort public static void Optimizesort (int[] arr) {int min; for (int i=0;i<arr.length-1;i++) {min =i; for (int j=i+1;j<arr.length;j++) {if (Arr[min]>arr[j]) {min = j; }} swap (arr,i,min); } }//Direct Insert Sort public static void Insertsort (int[] arr) {for (int i = 1; i < arr.length; i++) {// Starting with a 1-point mark, take out an empty position to the IF (Arr[i-1] > Arr[i]) {//one-time insert procedure int temp = Arr[i]; int j = i; Compare move position, because there is an empty position, not an element while (J > 0 && arr[j-1] > Temp) { ARR[J] = arr[j-1]; j--; }//Put in the appropriate position arr[j] = temp; }}} public static void Swap (int[] arr,int x,int y) {int temp = 0; temp = arr[x]; ARR[X] = Arr[y]; Arr[y] = temp; } public static void PrintArray (int[] arr) {System.out.print ("["); for (int i=0;i<arr.length;i++) {if (i!=arr.length-1) System.out.print (arr[i]+ ","); ElseSystem.out.println (arr[i]+ "]"); }} public static void Main (string[] args) {int[] a = new int[]{3,1,4,9,6,2}; PrintArray (a); Selectsort (a); PrintArray (a); Bubblesort (a); PrintArray (a); Optimizesort (a); PrintArray (a); Insertsort (a); PrintArray (a); Arrays.sort (a); PrintArray (a); } }
Summary of the elements of arrays in Java