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. 大数据学习——spark安装

    一主多从 1 上传压缩包 2 解压 -bin-hadoop2..tgz 删除安装包 -bin-hadoop2..tgz 重命名 mv spark-1.6.2-bin-hadoop2.6/ spark  ...

  2. Linux Shell系列教程之(七)Shell输出

    本文是Linux Shell系列教程的第(七)篇,更多shell教程请看:Linux Shell系列教程 与其他语言一样,Shell中也有输出操作,而且在实际应用中也是非常重要的,今天就为大家介绍下S ...

  3. 性能测试之七--jdbc

    jdbs用任意协议打开都行,具体脚本见下 在vuser_init里面 #include "Ptt_Mysql.h" vuser_init() { lr_load_dll (&quo ...

  4. [luoguP2766] 最长递增子序列问题(最大流)

    传送门 题解来自网络流24题: [问题分析] 第一问时LIS,动态规划求解,第二问和第三问用网络最大流解决. [建模方法] 首先动态规划求出F[i],表示以第i位为开头的最长上升序列的长度,求出最长上 ...

  5. NOIP2017赛前模拟(3):总结

    题目: 1.购买板凳(100) 大意:区间修改最后查询全局最大值; 2.新排序(40分暴力) 大意:给一串长度小于100000的数列···每次操作找出序列中严格小于其左边的数字或者严格大于其右边的数字 ...

  6. caffe编译新问题

    我在一台机子上,配置第二个caffe的时候,复制之前的Makefile文件,直接 make all 居然报错了报错如下 ndefined reference to cv::imread(cv::Str ...

  7. DataSet用法一:添加代码创建的表DataTable,设置主键外键,读取及修改DataSet表中数据

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  8. js simple drag.

    // by zhangxinxu welcome to visit my personal website http://www.zhangxinxu.com/ // zxx.drag v1.0 20 ...

  9. ng-include

    ng-include可以作为标签或者属性来使用,作用是引入公用文件. <div ng-include="'header.html'"></div> 注意里面 ...

  10. Scrapy学习-12-使用DownloaderMiddleware随机修改User-Agent

    随机替换请求头中的User-Agent 基于github开源项目,实现User-Agent的动态切换和管理 https://github.com/hellysmile/fake-useragent   ...