public class Test1 {
public static void main(String[] args) {
int[] arr = {3, 4, 6, 7, 89, 0};
//得到最大值和最小值
int max = getMax(arr);
int min = getMin(arr);
int[] newArr = new int[arr.length - 2];
int index = 0;//作为新数组的索引
for (int i = 0; i < arr.length; i++) {
if (arr != max && arr != min) {
newArr[index] = arr;
index++;
}
}
//打印输出新数组
System.out.println(Arrays.toString(newArr));
}
public static int getMin(int[] arr) {
int min = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] < min) {
min = arr[x];
}
}
return min;
}
/*
两个明确:
返回值类型:int
参数:int[] arr
*/
public static int getMax(int[] arr) {
int max = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] > max) {
max = arr[x];
}
}
return max;
}
} |
|