This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How is priority determined?-Collection Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How is priority determined?-Collection Doubt" Watch "How is priority determined?-Collection Doubt" New topic
Author

How is priority determined?-Collection Doubt

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361

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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
bing marquez
Greenhorn

Joined: Aug 06, 2006
Posts: 11
to get the sorted order, try this:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by bing marquez:
to get the sorted order, try this:



Or as the API suggests:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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

Joined: Aug 06, 2006
Posts: 11
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

Joined: Mar 04, 2006
Posts: 361
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

Joined: Aug 03, 2002
Posts: 7729
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
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

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

Joined: Mar 04, 2006
Posts: 361

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 03, 2002
Posts: 7729
Here's an example where you can use the Object[] returned from toArray():
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How is priority determined?-Collection Doubt