1、Arrays.sort(数组名)
对数组中的所有元素进⾏排序,并且是按照从⼩到⼤的顺序进⾏的。Integer[] test = new Integer[n];Arrays.sort(test);
2、Arrays.sort(数组名,起始下标,终⽌下标)
对数组中的部分元素进⾏排序,即从起始下标到终⽌下标-1的元素排序。
3、使⽤⽐较器⽐较的⽅法public class t6 { static int n;
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(\"请输⼊个数:\"); int n = sc.nextInt();
Integer[] test = new Integer[n]; for(int i = 0; i < n; i++) { test[i] = sc.nextInt(); }
MyComparator cmp = new MyComparator(); Arrays.sort(test, cmp);
for(Integer p: test) {
System.out.println(p); } }}
class MyComparator implements Comparator public int compare(Integer o1, Integer o2) { return o1 - o2; //从⼩到⼤ }} class MyComparator implements Comparator public int compare(Integer o1, Integer o2) { if(o1 > o2) { return 1; }else if (o1 < o2) { return -1; }else { return 0; } }} class MyComparator implements Comparator public int compare(Integer o1, Integer o2) { return o2 - o1; //从⼤到⼩ }} class MyComparator implements Comparator public int compare(Integer o1, Integer o2) { if(o1 < o2) { return 1; }else if (o1 > o2) { return -1; }else { return 0; } }} 因篇幅问题不能全部显示,请点此查看更多更全内容