class ArraysUtils {
public static String toString(int[] array) {
if (array == null || array.length == 0) {
return "[]";
}
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
if (i < array.length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
public static double getAverage(double[] array) {
if (array == null || array.length == 0) {
return 0.0;
}
int len = array.length;
if (len <= 2) {
return 0.0;
}
double sum = 0.0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double num : array) {
sum += num;
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
}
return (sum - min - max) / (len - 2);
}
}
public class Test {
public static void main(String[] args) {
int[] intArray = {10, 20, 30, 40, 50};
System.out.println(ArraysUtils.toString(intArray));
double[] doubleArray = {10.0, 20.0, 30.0, 40.0, 50.0};
System.out.println(ArraysUtils.getAverage(doubleArray));
int[] emptyArray = null;
System.out.println(ArraysUtils.toString(emptyArray));
double[] smallArray = {10.0};
System.out.println(ArraysUtils.getAverage(smallArray));
int[] emptyIntArray = {};
System.out.println(ArraysUtils.toString(emptyIntArray));
double[] emptyDoubleArray = {};
System.out.println(ArraysUtils.getAverage(emptyDoubleArray));
}
}
简单地继承和多态
最新推荐文章于 2024-09-02 22:29:33 发布