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. jmeter所有版本下载路径

    https://archive.apache.org/dist/jmeter/binaries/

  2. [python学习篇][廖雪峰][2][高级函数] map 和reduce

    我们先看map.map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数 ...

  3. -bash: ./start.sh: /bin/sh^M: bad interpreter: No such file or directory 错误解决方案

    问题描述:sh文件中,在win环境下,用WinSCP编辑,出现如下错误: -bash: ./start.sh: /bin/sh^M: bad interpreter: No such file or ...

  4. oracle中xhost报错

    一.命令找不到 xhost:command not found yum whatprovides "*/xhost" Loaded plugins: product-id, sec ...

  5. Welcome-to-Swift-19类型嵌套(Nested Types)

    枚举类型常被用于实现特定类或结构体的功能.也能够在有多种变量类型的环境中,方便地定义通用类或结构体来使用,为了实现这种功能,Swift允许你定义类型嵌套,可以在枚举类型.类和结构体中定义支持嵌套的类型 ...

  6. zoj 3790 Consecutive Blocks 离散化+二分

    There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in ...

  7. FZOJ Problem 2219 StarCraft

                                                                                                        ...

  8. 46深入理解C指针之---内存分析

    一.size_t:用于安全表示长度,所有平台和系统都会解析成自己对应的长度 1.定义:size_t类型表示C中任何对象所能表示的最大长度,是个无符号整数:常常定义在stdio.h或stdlib.h中 ...

  9. Linux 之 文件搜索命令

    文件搜索命令 参考教程:[千峰教育] 文件搜索定位 grep: 作用:通用规则表达式分析程序,是一种强大的文本搜索工具, 它能使用正则表达式搜索文本,并把匹配的行打印出来. 格式:grep [选项] ...

  10. LeetCode OJ——Convert Sorted Array to Binary Search Tree

    http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 将一个升序的数组转换成 height balan ...