First, the array of tools
Package com.atguigu.java; Import java.util.Arrays; / * * java.util.Arrays: array operation tools, many of which defines a method of operating an array * * * / public class ArraysTest { public static void main ( String [] args) { // 1.boolean the equals (int [] A, int [] B): determining whether the two arrays are equal. int [] of arr1 = new new int [] {1,2,3,4 }; int [] = arr2 is new new int [] {1,3,2,4 }; Boolean isEquals = that Arrays.equals (of arr1, arr2 is); System.out.println (isEquals); // false // 2.String toString (int [] A): the information output array. System.out.println (of Arrays.toString (of arr1)); // 3.void Fill (int [] A, int Val): The specified value is filled into the array. Arrays.fill (arr1,10 ); System.out.println (of Arrays.toString (of arr1)); // 4.void Sort (int [] A): sort the array. Arrays.sort (arr2 is); System.out.println (of Arrays.toString (arr2 is)); // 5.int binarySearch (int [] A, int Key) int [] = ARR3 new new int [] {- 98, - 34,2,34,54,66,79,105,210,333 }; int index = Arrays.binarySearch (ARR3, 210 ); IF (index> = 0 ) { System.out.println(index); }else{ System.out.println("未找到"); } } }
Second, the array of related algorithms
Package com.atguigu.java; / * * test algorithms: find the value of the maximum array elements, minimum, average, sum and the like * * define a one-dimensional array of type int, containing 10 elements, respectively assigned random integer number, * and the maximum value of all the elements and then, a minimum value, and the value of the average value and outputs it. * Requirements: All randomized double-digit number. * * [10,99] * formula: (int) (Math.random () * (99 - 10 +. 1) + 10) * * / public class ArrayTest1 { public static void main (String [] args) { int [ ] = ARR new new int [10 ]; for ( int I = 0; I <arr.length; I ++ ) { ARR [I] = ( int)(Math.random() * (99 - 10 + 1) + 10); } //遍历 for(int i = 0;i < arr.length;i++){ System.out.print(arr[i] + "\t"); } System.out.println(); //求数组元素的最大值 int maxValue = arr[0]; for(int i = 1;i < arr.length;i++){ if(maxValue < arr[i]){ maxValue = arr[i]; } } System.out.println("最大值为:" + maxValue); // find the minimum value of the array element int the minValue ARR = [0 ]; for ( int I =. 1; I <arr.length; I ++ ) { IF (the minValue> ARR [I]) { the minValue = ARR [I]; } } System.out.println ( "minimum value is:" + the minValue); // sum of array elements required int sUM = 0 ; for ( int I = 0; I <arr.length; I ++ ) { sUM + = ARR [ I]; } System.out.println ( "sum:" +SUM); // average number of array elements required int AvgValue = SUM / arr.length; System.out.println ( "Mean is:" + AvgValue); } }
Package com.atguigu.java; / * * test algorithms: an array replication, reverse lookup (linear search, binary search) * * * / public class ArrayTest2 { public static void main (String [] args) { String [] ARR = new new String [] { "JJ", "DD", "the MM", "BB", "GG", "AA" }; // copy an array (as distinct from the array variable assignment: arr1 = arr ) String [] of arr1 = new new String [arr.length]; for ( int I = 0; I <arr1.length; I ++ ) { of arr1 [I] = ARR [I]; } // reverse the array //方法一: // for(int i = 0;i < arr.length / 2;i++){ // String temp = arr[i]; // arr[i] = arr[arr.length - i -1]; // arr[arr.length - i -1] = temp; // } //方法二: // for(int i = 0,j = arr.length - 1;i < j;i++,j--){ // String temp = arr[i]; // arr[i] = arr[j]; // arr[j] = temp; // } //遍历 for(int i = 0;i < arr.length;i++){ System.out.print(arr[i] + "\ T" ); } System.out.println (); // Find (or search) // linear search: String = dest "BB" ; dest = "the CC" ; Boolean isFlag = to true ; for ( int I 0 =; I <arr.length; I ++ ) { IF (dest.equals (ARR [I])) { System.out.println ( "finds the specified element position:" + I); isFlag = to false ; BREAK ; } } IF (isFlag) { System.out.println ("Unfortunately, not found it!" ); } // binary searching :( familiar) // precondition: the array to be searched must be ordered. int [] = arr2 is new new int [] {- 98, -34,2,34,54,66,79,105,210,333 }; int dest1 = -34 ; dest1 = 35 ; int head = 0; // initial first index int End arr2.length = -. 1; // initial index of the last Boolean isFlag1 = to true ; the while (head <= end) { int Middle = (end head +) / 2 ; IF arr2 is [Middle]) {(dest1 == System.out.println ( "finds the specified element position:" + Middle); isFlag1 = to false ; BREAK ; } the else IF (arr2 is [Middle]> dest1) { End = Middle -. 1 ; } the else { // arr2 is [Middle] <dest1 head = Middle +. 1 ; } } IF (isFlag1) { System.out.println ( "Unfortunately, not found it!" ); } } }