File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 14433
    
  23

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 8 API documentation
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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: 10535
    
    9

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: 19785
    
  20

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: 14433
    
  23

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