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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 16:01:08

PriorityQueue ,ArrayList , 数组排序的相关文章

HashMap与Hashtable的分析

HashTable与HashMap概念 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.)此类不保证映射的顺序,特别是它不保证该顺序恒久不变.另外,HashMap是非线程安全的,也就是说在多线程的环境下,可能会存在问题,而Hashtable是线程安全的. 我们先来说HashTable  它的底层是用数据+链表实现的,无论key还是value都不能为

ACM等算法比赛中JAVA 常用&quot;STL&quot;总结:TreeMap,Queue,PriorityQueue等

第一个:显然是I/O的class啦~! /*IO相关*/ class InputReader { public InputReader() { // TODO Auto-generated constructor stub tokenizer = new StringTokenizer(""); reader = new BufferedReader(new InputStreamReader(System.in)); } public String nextTokenizer() t

ArrayList&amp;LinkedList&amp;Map&amp;Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

深入理解Java PriorityQueue

深入理解Java PriorityQueue PriorityQueue 本文github地址 Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示.本文从Queue接口函数出发,结合生动的图解,深入浅出地分析PriorityQueue每个操作的具体过程和时间复杂度,将让读者建立对PriorityQueue建立清晰而深入的认识. 总体介绍 前面以Java ArrayDeque为例讲解了Stack和Queue,其实还有一种特殊的队列叫做PriorityQueue,即优先

java常用集合类:Deque,ArrayList,HashMap,HashSet

Queue家族 无论是queue还是stack,现在常用的是Deque的实现类:如单线程的ArrayQueue,多线程的ArrayBlockingQueue Deque:读作“deck”,算是一种“双端队列”,即支持头部和尾部的数据访问和增删.----支持stack和queue的操作 关系:ArrayDeque --> Deque(I) -> Queue(I) -> Collection(I),其中->表示继承,-->表示实现,(I)表示接口. 关系:Stack ->

计算机程序的思维逻辑 (46) - 剖析PriorityQueue

上节介绍了堆的基本概念和算法,本节我们来探讨堆在Java中的具体实现类 - PriorityQueue. 我们先从基本概念谈起,然后介绍其用法,接着分析实现代码,最后总结分析其特点. 基本概念 顾名思义,PriorityQueue是优先级队列,它首先实现了队列接口(Queue),与LinkedList类似,它的队列长度也没有限制,与一般队列的区别是,它有优先级的概念,每个元素都有优先级,队头的元素永远都是优先级最高的. PriorityQueue内部是用堆实现的,内部元素不是完全有序的,不过,逐

VBScript脚本实现数组排序

1.需求:VBScript对数组的功能支持相对其他语言而言比较弱,然而在脚本应用中经常要使用数组,甚至对数组排序等,例如:遍历某个目录下的所有文件,并按某个规则依次保存到数组中. 2.实现方法:既然VBScript本身的功能不够,那么可以调用.NET Framework相关组件(mscoree.dll)的功能,具体实现如下: 1> 写个数组排序函数(参数:数组): Function aSortArray(Array)     Dim i,oArrayList, iElement,tempArra

转:PriorityQueue

转自:PriorityQueue 本文github地址 Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示.本文从Queue接口函数出发,结合生动的图解,深入浅出地 分析PriorityQueue每个操作的具体过程和时间复杂度,将让读者建立对PriorityQueue建立清晰而深入的认识. 总体介绍 前面以Java ArrayDeque为例讲解了Stack和Queue,其实还有一种特殊的队列叫做PriorityQueue,即优先队列.优先队列的作用是能保证每次取出的

Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack

(最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Interface) ->Collection(Interface). (Maybe因为Java中的数组本身就比较特殊?) 包含一些用来操作数组的一些方法,比如排序,搜索,复制,填充,toString方法等: 搜索使用二分搜索: 排序:使用DualPivotQuickSort中的排序算法,基本是改进版的快速排