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

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

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

    上节介绍了堆的基本概念和算法,本节我们来探讨堆在Java中的具体实现类 - PriorityQueue. 我们先从基本概念谈起,然后介绍其用法,接着分析实现代码,最后总结分析其特点. 基本概念 顾名思 ...

  2. Java中堆的实现类PriorityQueue队列接口Queue

    Application:这层的职责是对接收到的数据做一些非业务性验证,事务的控制,最重要的是协调多个聚合之间的操作.这里应该可以清晰的表达出整个操作所做的事情,并且与通用语言是一致的. 以上我们讲到可 ...

  3. PriorityQueue

    基本概念 顾名思义,PriorityQueue是优先级队列,它首先实现了队列接口(Queue),与LinkedList类似,它的队列长度也没有限制,与一般队列的区别是,它有优先级的概念,每个元素都有优 ...

  4. 深入理解Java PriorityQueue

    PriorityQueue 本文github地址 Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示.本文从Queue接口函数出发,结合生动的图解,深入浅出地分析Prio ...

  5. ArrayList&LinkedList&Map&Arrays

    Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将o ...

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

    (最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...

  7. Java数组排序

    Java数组排序Arrays.sort,以及Comparator接口的用法 有的时候需要对数组里的element进行排序.当然可以自己编写合适的排序方法,但既然java包里有自带的Arrays.sor ...

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

    图一:java collection 类图 Queue家族 无论是queue还是stack,现在常用的是Deque的实现类:如单线程的ArrayQueue,多线程的ArrayBlockingQueue ...

  9. ArrayList类

    /* * Collection是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的 * * Collection的功能概述 * 1添加功能 * boolean add(Object ob ...

随机推荐

  1. matlab callback 数据传递

    M文件中内的每个Callback都可以视为一个独立的可执行的接口,因此,任一个Callback触发后所执行的运算值若要在其他Callback中使用,就无法与MATLAB工作空间内的变量继续执行操作,也 ...

  2. BIT+DP

    2018CCPC网络赛 J - YJJ's Salesman HDU - 6447 YJJ is a salesman who has traveled through western country ...

  3. 2017"百度之星"程序设计大赛 - 初赛(A)

    小C的倍数问题  Accepts: 1990  Submissions: 4931  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 327 ...

  4. css3制作扇形菜单

    工作中网页中有一个扇形的导航菜单,以前没有接触过,参考了http://www.w3cplus.com/css3/building-a-circular-navigation-with-css-tran ...

  5. ACM程序设计选修课——1031: Hungar的得分问题(二)(杨辉三角+二进制转换)

    1031: Hungar的得分问题(二) 时间限制: 1 Sec  内存限制: 64 MB 提交: 15  解决: 10 [提交][状态][讨论版] 题目描述 距离正式选秀时间越来越近了,今天Hung ...

  6. 深入理解Java中的volatile关键字

    在再有人问你Java内存模型是什么,就把这篇文章发给他中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronized ...

  7. iOS-sqlite3&FMDB使用代码示范

    数据库操作是我们使用十分频繁的一份操作,在iOS中如何使用数据库,使用什么数据库,是我们不得不考虑的一个问题. 小型数据我们可以使用plist文件,或者NSUserDefaults存储.数据量比较多得 ...

  8. APUE 学习笔记(二) 文件I/O

    1. 文件I/O 对于内核而言,所有打开的文件都通过文件描述符引用,内核不区分文本文件和二进制文件 open函数:O_RDONLY  O_WRONLY  O_RDWR create函数: close函 ...

  9. Codevs 1501 二叉树的最大宽度和高度

    1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描 ...

  10. 标准C程序设计七---50

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...