class Day5_3 {
public static void main(String[] args) {
int [] arr ={6,2,9,15,1,5,20,7,18};
int max = arr[0];
int min = arr[0];
int d =0;
int x = 0;
for (int i = 0; i < arr.length ;i ++ ) { //遍历出最大值和最小值
if (max < arr[i]) {
max = arr[i];
}else if (min > arr[i]) {
min = arr[i];
}
}
//根据最大值,和最小值进行搜索,找出相对应的索引值
for (int i = 0;i < arr.length ;i++ ) {
if (max == arr[i]) {
d = i;
}else if (min == arr[i]) {
x = i;
}
}
//打印出最大值最小值以及相对应的索引
System.out.println(max);
System.out.println(min);
System.out.println(d);
System.out.println(x);
//取得了最大值和最小值的索引,进行交换;
int temp = arr[d];
arr[d] = arr[arr.length -1];
arr[arr.length-1] = temp;
temp = arr[x];
arr[x] = arr[0];
arr[0] = temp;
//打印新的数组
for (int i = 0; i < arr.length;i++ ) {
System.out.print(arr[i] + " ");
}