1. 程式人生 > >緊接著上篇文章,既然

緊接著上篇文章,既然

sco style () back -c 自己 new spa sta

技術分享


技術分享


註意描述:一個是插入隊列(1是可以插入尾部eg一遍的序列尾部追加,2是可以插入中間eg優先隊列)

而移除的描素都是刪除頭部

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class test {
    private String name;
    private int score;
    public test(String name, int population){
        this.name = name;
        
this.score = population; } public String getName(){ return this.name; } public int getScore(){ return this.score; } public String toString(){ return getName() + " : " + getScore(); //還有這種操作 } /*以上的javabean結束*/ public static void main(String args[]){ Comparator
<test> OrderIsdn = new Comparator<test>(){ //自己的意願進行優先級排列的隊列的話, public int compare(test o1, test o2) { // 需要實現Comparator接口。 // TODO Auto-generated method stub int numbera = o1.getScore(); int numberb = o2.getScore();
if(numberb > numbera){ return 1; }else if(numberb<numbera){ return -1; }else{ return 0; } } };//比較器結束 Queue<test> priorityQueue = new PriorityQueue<test>(11,OrderIsdn); test t1 = new test("t1",80); test t3 = new test("t3",88); test t2 = new test("t2",99); test t4 = new test("t4",87); priorityQueue.add(t1); priorityQueue.add(t3); priorityQueue.add(t2); priorityQueue.add(t4); // add可以添加到尾部,或者其他的如優先隊列(就看實現類怎麽實現) System.out.println(priorityQueue); //[t2 : 99, t4 : 87, t3 : 88, t1 : 80] while(!priorityQueue.isEmpty()) System.out.println(priorityQueue.poll().toString());//移除都是頭部 /** * t2 : 99 t3 : 88 t4 : 87 t1 : 80 */ } }

緊接著上篇文章,既然