| Author |
PriorityQueue()
|
Rizvan Asgarov
Ranch Hand
Joined: Jan 07, 2009
Posts: 443
|
|
Dear friends,
I hope everything is OK...
Such question arose in me about PriorityQueue when i test myself with K&B book: (page-641, question:9)
When i debugged this code i have seen that's why changes 2 places with 4 when add 1 to pg?
So i have not understood this subject evidently well.. Can you explain it please..
I thank at first
Rizvan
----------------------------------------------------------
My SCJP exam date approximately 15.03.2009
|
"Half of the science is to ask question"
|
 |
Rizvan Asgarov
Ranch Hand
Joined: Jan 07, 2009
Posts: 443
|
|
I corrected mistakes in the top code
Also i changed this code as following:
..................
When debugging and I can explain so: adds 2, then 4, then 1 but I see 2 and 4 changed places (say myself-ok, maybe 4 is greater than 2) but at the end of debugging 6 has been located the most at the end.(it ought to 7) Why?
And can you explain it ( PriorityQueue rules without Comparator) please?
Thanks,
Rizvan
----------------------------------------------------------
My SCJP exam date approximately 15.03.2009
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
|
Rizvan I would recommend you to look at the PriorityQueue documentation once and then tell us if you have any doubts...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Rizvan Asgarov
Ranch Hand
Joined: Jan 07, 2009
Posts: 443
|
|
Thanks a lot Ankit..
But i don't understand clearly.. therefore that write there ....depending on which constructor is used.(Comparable or Comparator)
But i have not used these there.. I used without them.
I don't understand that while adding numbers they sometimes chage places - sometimes doesn't change..
Can you help?
Thanks
Rizvan
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Rizvan PriorityQueue always stores objects in sorted order. If you create a PriorityQueue with a Comparator, then it is used to sort the queue otherwise the default sorting is used to sort the objects in the queue. So if the PriorityQueue is not created with a Comparator and the objects added to the PriorityQueue don't implement Comparable, then you'll get a ClassCastException. Basically what I want to say is that whether created with a Comparator or not, PriorityQueue will always keep its elements sorted. Try out these simple programs
1.
2.
3.
4.
|
 |
Rizvan Asgarov
Ranch Hand
Joined: Jan 07, 2009
Posts: 443
|
|
Dear friend Ankit,
Thanks you very much..
Questions that I gave are easy although.. but forgive me that
a) I am new at Java for 6 months
b) I do not know English excellently
c) I hurry to get the certificate
Up to this day I have met with difficulties in two subject especially(regular expressions(have been explained well) and above mentioned)
So I believe to the Allah (God) and I rely on to myself...
I wish successes too, you..
Sincerely,
Rizvan
PS: You are further my real friend..
My SCJP exam date approximately 15.03.2009
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Ohh so your exam is 3 days away. Best of luck dude . I hope Allah will listen to your prayers and you'll get a good score...
|
 |
 |
|
|
subject: PriorityQueue()
|
|
|