public static void main(String[] args) {
int[] a = {6,2,9,15,1,5,20,7,18};
int index_max = 0;
int index_min = 0;
System.out.println("交换前:");
for(int i = 0; i < a.length; i++ ) {
System.out.print(a[i] + " ");
}
System.out.println();
for(int i = 1; i < a.length; i++ ) {
if(a[index_max] < a[i]) {
index_max = i;
}else if(a[index_min] > a[i]) {
index_min = i;
}
}
int t = a[index_min];
a[index_min] = a[0];
a[0] = t;
t = a[index_max];
a[index_max] = a[a.length - 1];
a[a.length -1] = t;
System.out.println("交换后:");
for(int i = 0; i < a.length; i++ ) {
System.out.print(a[i] + " ");
}
}
这样就可以了 |