zoukankan      html  css  js  c++  java
  • android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)

    Info.java :

    public class Info implements Cloneable, Comparable<Info>{
    private int id ;
    public Info(int id ){
    this.id = id ;
    }
    
    public int getId() {
    return id;
    }
    
    @Override
       public Object clone() throws CloneNotSupportedException {
      Info info = (Info) super.clone();
      return info;
       }
    
    @Override
    public int compareTo(Info o) {
    if(this.id > o.id ){
    return 1 ;
    }
    return 0;
    }
    }





    在JAVA中 Collections.sort(List<T> list) 的测试:


    List<Info> list = new ArrayList<Info>() ;
    list.add( new Info(3)) ;
    list.add( new Info(2)) ;
    list.add( new Info(1)) ;
    list.add( new Info(4)) ;
    list.add( new Info(5)) ;
    Collections.sort(list) ;
    for(int i = 0 ; i < 5 ; i ++){
    System.out.println(list.get(i).getId());
    }


    结果:

    1

    2

    3

    4

    5


    在android 中 Collections.sort(List<T> list) 的测试:

    List<Info> list = new ArrayList<Info>() ;
    list.add( new Info(3)) ;
    list.add( new Info(2)) ;
    list.add( new Info(1)) ;
    list.add( new Info(4)) ;
    list.add( new Info(5)) ;
    Collections.sort(list) ;
    for(int i = 0 ; i < 5 ; i ++){
    System.out.println(list.get(i).getId());
    }


    结果:

    3

    2

    1

    4

    5

    在android中实现如果排序的方法可以用Collections.sort(List<T> list, new Comparator<Info>() {

    @Override
    public int compare(Info lhs, Info rhs) {
    return rhs.getId() - lhs.getId();
    }
      }

    可以实现对list的排序。

  • 相关阅读:
    [NOIP2013]花匠
    [NOIP2013]货车运输
    [NOIP2013]火柴排队
    [NOIP2012]疫情控制
    雷动WEBRTC产品
    WebRTC学习笔记_Demo收集
    Red5的直播与点播的压力测试(并发数的测试)
    Apache Tomcat8必备知识
    Servlet3.0学习总结(一)——使用注解标注Servlet
    一张图讲清楚高可用、高性能、可扩展的WEB系统架构
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207333.html
Copyright © 2011-2022 走看看