zoukankan      html  css  js  c++  java
  • 对List

     class MyCompare implements Comparator//自定义比较方式 要实现Conparator的 compare 方法
    {
        public int compare(Object o1,Object o2)//要比较的类型,这里要是(上帝类)
        {
      //
            int a=((Integer)o1).intValue();//Integer对象不能直接相比较  需要取得她的int值 来进行比较
            int b=((Integer)o2).intValue();
            if(a<b)return 1;     //从大到小排序
            if(a>b)return -1;
            return 0;
        }
    }
     
    public class SortList
    {
     
        public  static void output(List list)
        {
            if(list==null)
                return ;
            for(int i=0;i<list.size();i++)
                System.out.print(list.get(i).toString()+"  ");
            System.out.println();
        }
        public static void main(String[] args)
        {
            // TODO 自动生成的方法存根
            List list=new ArrayList();
            for(int i=0;i<20;i++)
            list.add(new Integer(20-i));
            output(list);
            Collections.sort(list);//此类可以对集合List排序 默认是按照升序进行排列的
            output(list);
            Collections.sort(list,new MyCompare());//按照自己定义的排序规则对List进行排序
            output(list);
        }
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    读取目录中文件名的方法 C++
    std::string::compare() in C++
    std::string::erase in C++ 之 erase()方法截取string的前几位、后几位
    Source Insight 破解
    bib+windows+word=bibtex4word插件使用方法
    7K7K小游戏
    Linux+Gurobi-python调用
    使用位运算替代模运算,为了高效做模除最好选择mod除(2^n)
    windows+VS2019+gurobi解决方案配置
    The plagiarism
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5709827.html
Copyright © 2011-2022 走看看