JavaSE-5.4【案例:数组遍历、数组最值】
1 package day5; 2 3 /** 4 * @author haifei 5 * 6 * 案例:数组遍历、数组最值 7 * 8 */ 9 10 public class demo4 { 11 public static void main(String[] args) { 12 printArr(); 13 getMaxMin(); 14 } 15 16 public static void printArr(){ 17 int[] arr = {11, 22, 33, 44, 55}; 18 doPrintArr(arr); 19 } 20 21 public static void doPrintArr(int[] arr){ 22 System.out.print("["); 23 for(int i=0; i<arr.length; i++){ 24 if(i == arr.length-1){ 25 System.out.print(arr[i]); 26 }else { 27 System.out.print(arr[i] + ", "); 28 } 29 } 30 System.out.println("]"); 31 } 32 33 public static void getMaxMin(){ 34 int[] arr = {12, 45, 3, 477, 69}; 35 int[] result = findMaxMin(arr); 36 System.out.println("max=" + result[0]); 37 System.out.println("min=" + result[1]); 38 } 39 40 public static int[] findMaxMin(int[] arr){ 41 int max = arr[0]; 42 int min = arr[0]; 43 44 for(int i=0; i<arr.length; i++){ 45 if(max < arr[i]){ 46 max = arr[i]; 47 } 48 if(min > arr[i]){ 49 min = arr[i]; 50 } 51 } 52 53 int[] resultArr = {max, min}; 54 return resultArr; 55 } 56 57 }