Priority Queues
来源:互联网 发布:上海雨人软件 编辑:程序博客网 时间:2024/05/17 04:38
- Priority Queue
- Nested Class Java嵌套类
- Example 1 without nested class
- Example 2 nested class
本篇文章主要介绍优先队列(Priority Queue)和嵌套类(nested class)的基本用法。
接下来的文章会介绍在实现例如Greedy Best First Search和AStar搜索算法中使用priority queue。
Priority Queue
In priority queue:
The order of the queue is controlled by implementing Comparable.
Nested Class: Java嵌套类
if you make an inner class static, this is commonly called a nested class.
create an inner class, you don’t need an outer-class object.
you can not access a non-static outer-class object from an object of a nested class.
Example 1 (without nested class)
- Here is an example to show how we can specify order using Priority Queue.
/** * an example of Priority Queue * Created by HuGuodong on 2017/10/25. */public class ToDoList extends PriorityQueue<ToDoList.ToDoItem>{ class ToDoItem implements Comparable<ToDoItem> { private char primary; private int secondary; private String item; public ToDoItem(char pri, int sec, String i) { this.primary = pri; this.secondary = sec; this.item = i; } /** * order by the first arg, then the second arg * @param o * @return */ @Override public int compareTo(ToDoItem o) { if (primary > o.primary) { return 1; } if (primary == o.primary) { if (secondary > o.secondary) { return 1; } else if (secondary == o.secondary) { return 0; } } return -1; } public String toString() { return Character.toString(primary) + secondary + ": " + item; } } public void add(char pri, int sec, String item){ super.add(new ToDoItem(pri, sec, item)); } public static void main(String[] args){ ToDoList toDoList = new ToDoList(); toDoList.add('c', 16,"Fog"); toDoList.add('c', 3,"Dog"); toDoList.add('a', 2,"Aog"); while(!toDoList.isEmpty()){ // remove is the same as poll, except its throw Exception, when it's empty. System.out.println(toDoList.remove()); //System.out.println(toDoList.poll()); }// result:// a2: Aog// c3: Dog// c16: Fog }}
Example 2 (nested class)
- Here is an example of using static inner class to implement PriorityQueue.
public class MyPriorityQueue extends PriorityQueue<MyPriorityQueue.Student>{ public static void main(String[] args){ MyPriorityQueue que = new MyPriorityQueue(); que.add(new Student(100, "guodong_hu")); que.add(new Student(99, "xie")); que.add(new Student(1, "lin")); while (!que.isEmpty()){ System.out.println(que.poll()); } } static class Student implements Comparable<Student>{ Integer no; String name; public Student(Integer no, String name){ this.no = no; this.name = name; } @Override public int compareTo(Student stu) { int result; if(no > stu.no){ result = 1; }else if(no == stu.no){ result = 0; }else{ result = -1; } return result; } @Override public String toString() { return "Student{" + "no=" + no + ", name='" + name + '\'' + '}'; } }}
阅读全文
0 0
- priority queues
- Priority Queues
- heap with priority queues
- C++ STL Priority Queues
- Heap and Priority Queues
- C++ Queues(队列)、Priority Queues(优先队列)
- C++ Queues(队列)、Priority Queues(优先队列)
- !!!Chapter 6 Priority Queues (Heaps)
- Algorithm Part I:Priority Queues
- Priority Queues优先级队列详解
- Algorithms 学习笔记05 Priority Queues
- Algorithms - Week 4-0 Priority Queues
- Java学习笔记(20) Lists, Stacks, Queues, and Priority Queues
- C++ Priority Queues(优先队列) and C++ Queues(队列)
- 标准模板库STL中优先队列Priority Queues使用手册
- 堆排序的应用 Priority queues 优先级排序
- Princeton Algorithms: Part 1 [week 4: Priority Queues]
- Dplasma调度方式—— DAGUE_SCHEDULER_PBQ(Priority Based Queues)
- ACM训练半周总结—10月26
- 内存泄漏
- [刷题]HDU3157
- TensorFlow 全连接网络实现
- CentOS 7搭建VPN虚拟局域网服务器
- Priority Queues
- 中介者模式学习和思考
- HTML插入图片存储路径问题
- C++中友元及继承
- Docker的安装
- golang基础-结构体、结构体链表前后插入、节点添加删除
- codeforces 702A Maximum Increase
- SVN的使用总结
- 我的电路实践