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
9PriorityQueue ,ArrayList , 数组排序的更多相关文章
- 计算机程序的思维逻辑 (46) - 剖析PriorityQueue
上节介绍了堆的基本概念和算法,本节我们来探讨堆在Java中的具体实现类 - PriorityQueue. 我们先从基本概念谈起,然后介绍其用法,接着分析实现代码,最后总结分析其特点. 基本概念 顾名思 ...
- Java中堆的实现类PriorityQueue队列接口Queue
Application:这层的职责是对接收到的数据做一些非业务性验证,事务的控制,最重要的是协调多个聚合之间的操作.这里应该可以清晰的表达出整个操作所做的事情,并且与通用语言是一致的. 以上我们讲到可 ...
- PriorityQueue
基本概念 顾名思义,PriorityQueue是优先级队列,它首先实现了队列接口(Queue),与LinkedList类似,它的队列长度也没有限制,与一般队列的区别是,它有优先级的概念,每个元素都有优 ...
- 深入理解Java PriorityQueue
PriorityQueue 本文github地址 Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示.本文从Queue接口函数出发,结合生动的图解,深入浅出地分析Prio ...
- ArrayList&LinkedList&Map&Arrays
Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将o ...
- Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack
(最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...
- Java数组排序
Java数组排序Arrays.sort,以及Comparator接口的用法 有的时候需要对数组里的element进行排序.当然可以自己编写合适的排序方法,但既然java包里有自带的Arrays.sor ...
- java常用集合类:Deque,ArrayList,HashMap,HashSet
图一:java collection 类图 Queue家族 无论是queue还是stack,现在常用的是Deque的实现类:如单线程的ArrayQueue,多线程的ArrayBlockingQueue ...
- ArrayList类
/* * Collection是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的 * * Collection的功能概述 * 1添加功能 * boolean add(Object ob ...
随机推荐
- python基础学习笔记——类的约束
⾸先, 你要清楚. 约束是对类的约束. 用一个例子说话: 公司让小明给他们的网站完善一个支付功能,小明写了两个类,如下: class QQpay: def pay(self,money): print ...
- luogu2765 魔术球问题
发现好像没人来证明贪心啊--那我来写一下它的证明 欲证明:放一个数在已有的柱上(如果可以)总是比新开一个柱更优的 假如已经放了x1..x2....xu..xv..xw.... 现在我要放xx 我有两种 ...
- 【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸
多维数组声明 数据类型[][] 数组名称; 数据类型[] 数组名称[]; 数据类型数组名称[][]; 以上三种语法在声明二维数组时的功能是等价的.同理,声明三维数组时需要三对中括号,中括号的位置可以在 ...
- 全套Office办公软件WORD/PPT/EXCEL视频教程 每日更新中
详情见Processon分享链接:https://www.processon.com/view/link/5b3f40abe4b09a67415e2bfc
- day04_01 知识回顾、算术运算符
","和"+"的区别 除法运算,整除//,别名"地板除" 取余数 2**10 2的10次方 指数运算 指数运算符优先级要比乘法要高,所以先算 ...
- 理解Linux虚拟文件系统VFS
当前,除了linux标准的文件系统Ext2/Ext3/Ext4外,还有很多种文件系统,比如reiserfs, xfs, Windows的vfat NTFS,网络文件系统nfs 以及flash 文件系统 ...
- Struts2报错:No result defined for action xxx and result input
case如下: 1. 后台程序要升级, 修改了一些功能,但是没有修改或者添加action的参数. 2. 数据库需要升级,执行了一些sql,修改过action的值. 3. 当修改某个已经存在的记录,然后 ...
- P2564 生日礼物
生日礼物 洛谷链接 题目描述: 在一段彩带上有不同颜色的彩珠,求出包含所有颜色彩珠的最短彩带长度. 思路: 我们可以把按彩珠的位置把所有彩珠排一下序,然后从1开始遍历这些彩珠,并记录出现过的颜色数目, ...
- 用Javascript实现图片的缓慢缩放效果
<body> <!--页面布局:一张图片两个按钮--> <div style = "width:400px;margin:0 auto"> &l ...
- ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)
1057: Beautiful Garden Time Limit: 5 Sec Memory Limit: 128 MB Submit: 25 Solved: 12 [Submit][Statu ...