• 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

How is priority determined?-Collection Doubt

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

Im adding 'B' first then 'C' and then 'A'. I get the output ACB, but that is LIFO structure. So is priority Que oppiste of FIFO then?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amitabha Batranab:

Im adding 'B' first then 'C' and then 'A'. I get the output ACB, but that is LIFO structure. So is priority Que oppiste of FIFO then?



A priority queue returns the element in the order specified by the comparator -- or natural order if no comparator is provided.

However, this only applies to the methods of the Queue interface. In your case, you are using a "for" loop that uses the iterator to go through the queue. The iterator doesn't return the elements of the queue in any particular order.

Henry
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to get the sorted order, try this:
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bing marquez:
to get the sorted order, try this:




Or as the API suggests:
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I used Barry's code and updated below:
but the output is still ACB

Also I thought that Sets and lists had access to the toArray(). How come priorityQueue is getting to use it without a compiler error?
 
bing marquez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried 2 separate calls(*) and I was able to to get the sorted output of "ABC".

Nonetheless, Arrays.sort(s.toArray()) should have work as stated by Barry.
So, this is puzzling...

BDW PriorityQueue implements Que which in turn extends Collection, therefore it can use toArray().
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Puzzling indeed, kudos on getting it to work though. But now how come if we use the alternate form of toArray() it ceases to work? I get an exception.


 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Previously I quoted from the API but I did not check it. However, the following code works:


Which I realize is what bing did.

The problem is caused by the fact that Arrays.sort() sorts in place and does not return the array being sorted.
[ September 04, 2006: Message edited by: Barry Gaunt ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amitabha Batranab:
Puzzling indeed, kudos on getting it to work though. But now how come if we use the alternate form of toArray() it ceases to work? I get an exception.



The toArray() method that takes no parameters, returns an Object array -- not an array type that is past in and (may be different array object but same type) returned, that subclasses the Object array.

It is an Object array, even though all array elements are String elements. This means when you try to cast it to a String array -- it will throw an exception.

Henry
[ September 04, 2006: Message edited by: Henry Wong ]
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The toArray() method that takes no parameters, returns an Object array -- not an array type that is past in and (may be different array object but same type) returned, that subclasses the Object array.

It is an Object array, even though all array elements are String elements. This means when you try to cast it to a String array -- it will throw an exception.



So basically When can we use the alternate form of toArray() that takes no parameter? The way I see it is that it is useless?. But one thing Im still stumped on is that by doing 'String[] str = (String[])s.toArray()' we are downcasting,yes, but downcasting to the correct type? so why would this give exceptions.?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this simple example:


That is, just because you create an Object array (Object[]) and fill it with Strings does not mean that it is really a String array (String[]).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example where you can use the Object[] returned from toArray():
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic