zoukankan      html  css  js  c++  java
  • java,sort的数组降序

    1.Array.sort(数组,起始位置,结束位置)。这个是升序排序。

    2.关于数组的降序实现如下:

      利用Collections.reverseOrder()方法:

    import java.util.Arrays;
    import java.util.Collections;
     
    public class Main
    {
        public static void main(String[] args) 
        {
            int[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
            Arrays.sort(a,Collections.reverseOrder());
        }
    }

      实现Comparator接口的复写compare()方法:

    代码实现如下:

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    class Cmp1 implements Comparator<Integer>
    {
        public int compare(Integer o1, Integer o2) 
        {
            if(o1 < o2)
            {
                return 1;
            }
            else if(o1 == o2)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
        
    }
    public class Main
    {
        static final int MAX = 105;
        static Integer goods[] = new Integer[MAX];
        public static void main(String[] args)
        {
            Scanner cin = new Scanner(System.in);
            while(cin.hasNext())
            {
                int N = cin.nextInt();
                int c1 = cin.nextInt();
                int c2 = cin.nextInt();
                for(int i = 0; i < N; i++)
                {
                    goods[i] = cin.nextInt();
                }
                Arrays.sort(goods,0,N,new Cmp1());
                for(int i = 0; i < N; i++)
                {
                    System.out.println(goods[i]);
                }
            }
        }
    }
  • 相关阅读:
    项目01-nginx模块
    Spark机器学习
    项目01-手机端模块
    Spark内存管理
    Spark Streaming
    Spark SQL
    Spark Job调度
    Spark master节点HA配置
    机器学习
    07、Spark集群的进程管理
  • 原文地址:https://www.cnblogs.com/674001396long/p/10268417.html
Copyright © 2011-2022 走看看