• 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

Question from Niko's blog on Collections.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I don't understand why the answer to this question is c. What I understand from the code is that queue.toArray() returns an Object[] which isn't bounded to the collection. Therefore, it shouldn't affect the output and since PriorityQueue is sorted by natural order, in my opinion the otuput should be A A J V. Obviously I'm missing something...

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex, as you might know, when you use the enhanced for loop on a Collection, then actually the for loop iterates over the collection using the iterator. So if you read the documentation of the iterate method of PriorityQueue class, then it says

Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know that. I thought that when you used the iterator on a Collection that was sorted the iterator would follow the same order.
Thank you Ankit.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic