This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "query" Watch "query" New topic
Author

query

Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
for (PriorityQueue p:pq)


Why can't we use For-Each loop like this?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

What type is pq?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
pq is also priority queue. And i was trying to print all elements of priority queue. It was'nt working.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: query