以下列出三种用法
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
Arrays.sort(a); //对一个数组的所有元素进行排序,并且是按从小到大的顺序。
for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + " ");
}
System.out.println();
int[] b = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
Arrays.sort(b,0,6);//数组b的下标从0到6的元素排序,注意:下标为6的元素不参与排序
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+" ");
}
System.out.println();
char[] cs={'c','f','a','k','e','b','n','m'};
Arrays.sort(cs);//也可对字符数组进行排序
for (int i = 0; i < cs.length; i++) {
System.out.print(cs[i]+" ");
}
}
}
运行结果
0 1 2 3 4 5 6 7 8 9
2 3 4 7 8 9 1 0 6 5
a b c e f k m n