form: int [] nums = {7,2,8,9,1,12};
Jie Yi: 22 Compare and record the subscript, the next comparison with the last comparison of the maximum value and the last comparison of the next comparison, the cycle to find the maximum value
1 /**2 * @authorhorse-to-peak comparison find the maximum value again3 * @paramarr4 * @return5 */6 Private Static intGetmaxnum (int[] arr) {7 8 //Record the subscript9 intindex = 0;Ten //Suppose the first one is the maximum value One intmax = Arr[0]; A for(inti = index; i < arr.length; i++) { - if(i + 1 < arr.length && Max < Arr[i + 1]) { -index = i + 1; themax = arr[i + 1]; - } - } - returnMax; +}View Code
Solution Two: Bubble Sort method
1 Private Static intFindmaxnum (int[] arr) {2 3 for(inti = 0; i < arr.length; i++) {4 for(intj = 0; J < arr.length-i-1; J + +) {5 if(Arr[j] < Arr[j + 1]) {6 inttemp =Arr[j];7ARR[J] = arr[j + 1];8Arr[j + 1] =temp;9 }Ten } One } A - returnArr[0]; -}View Code
Java finds the maximum value of a set of numbers