File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PriorityQueue Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "PriorityQueue Problem" Watch "PriorityQueue Problem" New topic
Author

PriorityQueue Problem

mohitkumar gupta
Ranch Hand

Joined: May 18, 2010
Posts: 516



PROBLEM-1

code1


OPTIONS:

1.It prints J A V A.
2.It prints A A J V.
3.The output order is not guaranteed.

correct answer:3.

code2


OUTPUT:


It prints A A J V


MY Question is

why the code1 doesnot give same output as code2 because JLS says:

This class(PRIORITY QUEUE) and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray())


------------------------------------------------------------------------------------


PROBLEM-2

codeA


the above throws Exception at run time.

now see this code
codeB


the output of this program is
A
A J V


--------

when i uncomment line 7,then output is
A
J V


QUERIES:

a.why the line 6 doesnot display true or false ?
b.why is not a exception thrown as in the program codeA ?
c.why is the line 7 removes element from the queue as JLS SAYS:

remove
public boolean remove(Object o)Removes a single instance of the specified element from this queue, if it is present. More formally, removes an element e such that o.equals(e), if this queue contains one or more such elements. Returns true if and only if this queue contained the specified element (or equivalently, if this queue changed as a result of the call).

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>
Parameters:
o - element to be removed from this queue, if present
Returns:
true if this queue changed as a result of the call

This message was edited 4 times. Last update was at by mohitkumar gupta



OCPJP 6.0 93 %
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
I can answer your first question : the toArray method without any argument returns an Array of objects so there no natural order for sorting objects and we didn't specify any comparator,therefore the output is not guaranteed.
In the other case toArray(String[]) returns an array of strings and the sorting is done according to natural order.
Priority Queue allows duplicates so we get AAJV.

Hope it helps.


SCJP 93 % ,preparing for SCWCD
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9023

For problem 1, the toArray method of PriorityQueue class returns an Array which is not connected to the queue. So modifications to the array don't effect the PriorityQueue. When you iterate a PriorityQueue, the order of elements is not guaranteed. PriorityQueue maintains an order of elements when you call the peek or poll methods. While iterating a PriorityQueue, the elements are not iterated in sorted order.
In the second case you are iterating over an array. Since the array is sorted, the output is sorted.

For your second problem, you are looking at the description of boolean remove(Object o) method while you are calling the E remove() method of AbstractQueue class which says
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.
This implementation returns the result of poll unless the queue is empty.

So the remove() method returns the removed element not true or false...


Javaranch SCJP FAQ | SCWCD Links
 
 
subject: PriorityQueue Problem
 
MyEclipse, The Clear Choice