• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Poll() method

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags.

And you might want to check out java.util.PriorityQueue
 
suavedeep kaur
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob
 
reply
    Bookmark Topic Watch Topic
  • New Topic