import java.util.Arrays;
import java.util.Random;
public class Test {
public static void main(String[] args) {
int array[] = {1,4,5,3,2,0};
//没有排序前的输出
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
System.out.println();
Arrays.sort(array);//进行排序
//排序后的输出
for(int j = 0; j< array.length; j++){
System.out.print(array[j]);
}
}
}
这样条件就都满足了,需要改进可以给我发消息。
import java.util.*;
public class Test {
public static void main(String args[]){
int[] array = {1,4,5,3,2,0};
System.out.print("Original array: ");
for(int i=0;i<6;i++){
if (i==5) System.out.println(array[i]);
else System.out.print(array[i]+", ");
}
Arrays.sort(array);
System.out.print("Sorted array: \t");
for(int i=0;i<6;i++){
if (i==5) System.out.println(array[i]);
else System.out.print(array[i]+", ");
}
}
}
输出结果:
Original array: 1, 4, 5, 3, 2, 0
Sorted array: 0, 1, 2, 3, 4, 5
import java.util.Arrays;
public class test {
public static void main(String args[]) {
int[] num = {1,4,5,3,2,0};
for(int i:num){
System.out.print(i+"\t");
}
Arrays.sort(num);
System.out.println("\n排序后的结果");
for(int i:num){
System.out.print(i+"\t");
}
}
}
/*JDK1.5以上支持for-each循环*/