Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

query  RSS feed

 
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
Sheriff
Posts: 16026
87
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
Bartender
Posts: 11445
18
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
Bartender
Posts: 11445
18
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
Posts: 21088
85
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
Sheriff
Posts: 16026
87
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
Boost this thread!