1 /*2 Java Development process: ideas. 3 4 case: An array operation. Gets the maximum value. 5 Ideas:6 1, a group of numbers, to get the maximum value, compare. 7 2, how to compare? Compare each of the data in the array to get a comparison. 8 3, compare, record the relatively large data, and use this large data and the next9 data for comparison. Ten 4, if all the data is compared, then the larger value of the last record is the maximum value. One A steps: - 1, a class is required. - 2, this class needs to be run independently and must have the main method. the 3, an array is required. - 4, each time the larger data may be different, need a variable. - 5, get the number in the array. Need to traverse. - 6, in the process of traversal, need to compare, judge. + 7, every time the judgment condition is met, the larger value is recorded with the variable. Variable storage. - */ + A //Code: at //1, a class is required. - classArraydemo - { - //2, this class needs to be run independently and must have the main method. - Public Static voidMain (string[] args) - { in //3, an array is required. - int[] arr = {23,12,18,0,1,9,56}; to + //4, each time the larger data may be different, need a variable. - intMaxValue = arr[0];//initialized to any one of the elements in the array. the * //5, get the number in the array. In order to compare. Need to traverse. $ for(inti = 1;i<arr.length;i++)Panax Notoginseng { - //6, in the process of traversal, need to compare, judge. the //7, every time the judgment condition is met, the larger value is recorded with the variable. Variable storage. + if(Arr[i] >maxValue) A { theMaxValue = Arr[i];//as long as the traversed element is greater than the MaxValue, use + //MaxValue record that larger value. - } $ } $ //8, once the traversal is over, the maximum value in the array is stored in the MaxValue. - //9, in order to show the maximum value, tell the user. The output statement. -SYSTEM.OUT.PRINTLN ("Maximum value;" +maxValue); the } - }Wuyi the - /* Wu Case 2: another array. Also get the maximum value. Copy the code and change the variable. Trouble. - Since the code does not change, why not improve the reusability of this code? Reusability. About The code in Java becomes a whole, represented by a function. So a function is a function in a class. $ */ - classArrayDemo2 - { - //The main function. (The main function is used to test the function.) ) A Public Static voidMain (string[] agrs) + { the int[] arr2 = {23,12,18,0,1,90,56}; - $ //getting the maximum value requires a feature. the intMaxValue2 =Getmaxvalue (ARR2); theSystem.out.println ("MaxValue2:" +maxValue2); the } the - //function. in Public Static intGetmaxvalue (int[] arr) the { the intMaxValue = arr[0]; About the for(inti = 1;i<arr.length;i++) the { the if(Arr[i] >maxValue) + { -MaxValue =Arr[i]; the }Bayi } the //returns the maximum value. the returnMaxValue; - } - } the the the /* the Case 3: Continue the upgrade. There are many classes in which there are arrays. All need to get their maximum value. - the The ability to manipulate the maximum value is individually encapsulated in a class. the This allows you to use this feature as long as you find the class that the feature belongs to. the 94 all of the features are in the object and can be used as long as the object is found. the */ the the classArrayDemo398 { About Public Static voidMain (string[] agrs) - {101 int[] Arr3 = {23,12,180,0,1,90,56};102 103Arraytool tool =NewArraytool ();104 the //gets the maximum value. 106 intMaxValue3 =Tool.getmaxvalue (ARR3);107System.out.println ("MaxValue3:" +maxValue3);108 109 //int maxVlaue3 = Arraytool.getmaxvalue (ARR3); the 111 //gets the minimum value. the intMinValue =Tool.getminvalue (ARR3);113System.out.println ("MinValue:" +minValue); the } the } the 117 classArraytool118 {119 //gets the maximum value. - Public Static intGetmaxvalue (int[] arr)121 {122 intMaxValue = arr[0];123 124 for(inti = 1;i<arr.length;i++) the {126 if(Arr[i] >maxValue)127 { -MaxValue =Arr[i];129 } the }131 returnMaxValue; the }133 134 //gets the minimum value. 135 Public /*Static*/ intGetminvalue (int[] arr)136 {137 //code ...138 return0;139 } $ 141 //two-point lookup. Binary Lookup method. 142 Public /*Static*/ intBinarySearch (int[] arr,intkey)143 {144 //code ...145 return0;146 }147}
"Day06---Object-oriented introduction"