1 /** 2 * 构造器私有化 3 */ 4 private ArraysUtils(){} 5 6 // toString()工具类 静态方法、工具方法 7 public static String toString(int[] arr){ 8 String result = "["; 9 for (int i = 0; i < arr.length; i++) { 10 result += (i == arr.length -1 ? arr[i] : arr[i] + ", "); 11 } 12 result += "]"; 13 return result; 14 } 15 16 // 求平均分。静态方法工具方法 17 public static double average(int[] arr){ 18 int max = arr[0]; 19 int min = arr[0]; 20 int mean = 0; 21 for (int i = 0; i < arr.length; i++) { 22 if (arr[i] > max){ 23 max = arr[i]; 24 } 25 if (arr[i] < min){ 26 min = arr[i]; 27 } 28 mean += arr[i]; 29 } 30 31 return (mean - max - min) / (arr.length - 2) * 1.0; 32 }
1 public static void main(String[] args) { 2 int[] arr = {10, 20, 30}; 3 System.out.println(arr); 4 System.out.println(ArraysUtils.toString(arr)); 5 System.out.println(ArraysUtils.average(arr)); 6 7 }
标签:arr,min,int,平均数,toString,result,max,工具 From: https://www.cnblogs.com/dsds/p/17978143