This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Poll()   method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Poll()   method" Watch "Poll()   method" New topic
Author

Poll() method

suavedeep kaur
Ranch Hand

Joined: Jun 02, 2008
Posts: 36
import java.util.*;
class PQ{
static class PQSort implements Comparator<Integer>{
public int compare(Integer one , Integer two){
return two-one;
}

}

public static void main(String [] args){
int[] ia={1,5,3,7,6,9,8};
PriorityQueue<Integer> pq1= new PriorityQueue<Integer>();


for(int x: ia )
pq1.offer(x);
for(int x: ia )
System.out.print(pq1.poll() + " ");
System.out.println(" ");
PQSort pqs =new PQSort();//Comparator orders the elements in opposite of the natural order
PriorityQueue<Integer> pq2= new PriorityQueue<Integer>(10,pqs);


for(int x : ia)
pq2.offer(x);
System.out.println("size " + pq2.size());
System.out.println("peek " + pq2.peek());
System.out.println("size " + pq2.size());
System.out.println("poll " + pq2.poll());
System.out.println("size " + pq2.size());
for(int x : ia)
System.out.print(pq2.poll() + " ");
}

}


I am getting little confused about what the second for loop is doing in this program ??? If anyone can help !!!



Suavedeep kaur
SCJP
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Please Use Code Tags.

And you might want to check out java.util.PriorityQueue


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
suavedeep kaur
Ranch Hand

Joined: Jun 02, 2008
Posts: 36
thanks Rob
 
Consider Paul's rocket mass heater.
 
subject: Poll() method
 
Similar Threads
Collections..
priority queue!
Priority queue
Priority Queue
question regarding PriorityQueue