• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query

 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (PriorityQueue p:pq)


Why can't we use For-Each loop like this?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ofcourse you can use for-each loops like that, if pq is an Iterable<PriorityQueue>.

Please explain your question in more detail if you want a more detailed answer.
 
Maneesh Godbole
Saloon Keeper
Posts: 11173
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type is pq?
 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pq is also priority queue. And i was trying to print all elements of priority queue. It was'nt working.
 
Maneesh Godbole
Saloon Keeper
Posts: 11173
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Jesper said, PriorityQueue does indeed implement Iterable.
Can you explain by what you mean by
It was'nt working.

Do you get an exception? Are no results obtained when you run the loop? Is it because the queue is empty? Please TellTheDetails
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop tries to loop over all priority queues inside the priority queue. Unless you have a PriorityQueue<PriorityQueue<X>> (with X being some type), that's not going to work.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, what you are doing wrong is that you are using PriorityQueue for the type of p - that is most likely a mistake (unless pq really is a PriorityQueue that contains PriorityQueues, as Rob explains, but it's probably not what you meant).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic