zoukankan      html  css  js  c++  java
  • PriorityQueue ,ArrayList , 数组排序

     static class E implements Comparable<E>{
                         int x ;
                         int y ;
                         int state ;
                         int money ;
                         public E(int x , int y , int state , int money){
                                     this.x  = x ;
                                     this.y = y ;
                                     this.state = state ;
                                     this.money = money ;
                         }
    
                        @Override
                        public int compareTo(E o) {
                                    return money - o.money ;
                        }
              } 
    
    
    
    
    PriorityQueue<E> q = new PriorityQueue<E>() ;
                            q.add(new E(1, 0, 0, 1)) ;
                            q.add(new E(2, 0, 0, 3)) ;
                            q.add(new E(3, 0, 0, 2)) ;
                            q.add(new E(4, 0, 0, 5)) ;
                            q.add(new E(5, 0, 0, 9)) ;
                            while(! q.isEmpty()){
                                    System.out.println(q.poll().money) ;
                            }
    
    输出结果:
    1
    2
    3
    5
    9
    
    
    
    ArrayList<E> q = new ArrayList<E>() ;
                            q.add(new E(1, 0, 0, 1)) ;
                            q.add(new E(2, 0, 0, 3)) ;
                            q.add(new E(3, 0, 0, 2)) ;
                            q.add(new E(4, 0, 0, 5)) ;
                            q.add(new E(5, 0, 0, 9)) ;
    
                            Collections.sort(q) ;
                            for(E e : q){
                                    System.out.println(e.money) ;
                            }
    
    输出结果:
    1
    2
    3
    5
    9
    
    
      E[] q = new E[100]  ;
                            q[0] = new E(1, 0, 0, 1) ;
                            q[1] = new E(2, 0, 0, 3) ;
                            q[2] = new E(3, 0, 0, 2) ;
                            q[3] = new E(4, 0, 0, 5) ;
                            q[4] = new E(5, 0, 0, 9) ;
    
                            Arrays.sort(q , 0 , 5) ;
                            for(int i = 0 ; i < 5 ; i++){
                                    System.out.println(q[i].money) ;
                            }   
    输出结果:
    1
    2
    3
    5
    9
    
    
    
              static class E{
                         int x ;
                         int y ;
                         int state ;
                         int money ;
                         public E(int x , int y , int state , int money){
                                     this.x  = x ;
                                     this.y = y ;
                                     this.state = state ;
                                     this.money = money ;
                         }
              } 
    
    ArrayList<E> q = new ArrayList<E>() ;
                  q.add(new E(1, 0, 0, 1)) ;
                  q.add(new E(2, 0, 0, 3)) ;
                  q.add(new E(3, 0, 0, 2)) ;
                  q.add(new E(4, 0, 0, 5)) ;
                  q.add(new E(5, 0, 0, 9)) ;
    
                  Collections.sort(q ,   new Comparator<E>() {
                      @Override
                        public int compare(E a , E b) {
                                    return a.money - b.money ;
                        }
                   }
                  ) ;
    
                  for(E e : q){
                                    System.out.println(e.money) ;
                 }
    输出结果:
    1
    2
    3
    5
    9
    
    
     E[] q = new E[100]  ;
                q[0] = new E(1, 0, 0, 1) ;
                q[1] = new E(2, 0, 0, 3) ;
                q[2] = new E(3, 0, 0, 2) ;
                q[3] = new E(4, 0, 0, 5) ;
                q[4] = new E(5, 0, 0, 9) ;
    
                Arrays.sort(q, 0, 5, new Comparator<E>() {
                    @Override
                    public int compare(E a, E b) {
                                return  a.money - b.money ;
                    }
                }) ;
    
                  for(int i = 0 ; i < 5  ; i++){
                                    System.out.println(q[i].money) ;
                 }
    输出结果:
    1
    2
    3
    5
    9
    
     PriorityQueue<E> q = new PriorityQueue<E>(1 , new Comparator<E>() {
                      @Override
                        public int compare(E a , E b) {
                                    return a.money - b.money ;
                        }
                   }) ;
                  q.add(new E(1, 0, 0, 1)) ;
                  q.add(new E(2, 0, 0, 3)) ;
                  q.add(new E(3, 0, 0, 2)) ;
                  q.add(new E(4, 0, 0, 5)) ;
                  q.add(new E(5, 0, 0, 9)) ;    
                  while(! q.isEmpty()){
                                    System.out.println(q.poll().money) ;
                 }
    
    输出结果:
    1
    2
    3
    5
    9
  • 相关阅读:
    2、细节&Class对象
    1、概述&应用场景
    Magento请求分发与控制器
    Magento强大的配置系统
    ecshop在PHP 5.4以上版本各种错误问题处理
    Thinkphp单字母函数使用指南
    五种常见的 PHP 设计模式
    MyISAM和InnoDB的区别
    linux下如何删除文件夹
    Linux软件安装与卸载
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8626346.html
Copyright © 2011-2022 走看看