数组的排序
例子:将数组排序程序修改成一个方法的调用形式
package WordCount;
import javax.naming.ldap.SortControl;
public class ArrayRefDemo03 {
public static void main(String[] args) {
int score[] = { 67, 89, 87, 69, 90, 100, 75, 90 };
int age[] = { 31, 30, 18, 17, 8, 9, 1, 39 };
sort(score);
print(score);
System.out.println("\n");
sort(age);
print(age);
}
public static void sort(int temp[]) {
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp.length; j++) {
if (temp[i] < temp[j]) {
int x = temp[i];
temp[i] = temp[j];
temp[j] = x;
}
}
}
}
public static void print(int temp[]) {
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "\t");
}
}
}
结果:
67 69 75 87 89 90 90 100
1 8 9 17 18 30 31 39
浙公网安备 33010602011771号