This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PriorityQueue Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PriorityQueue Question" Watch "PriorityQueue Question" New topic
Author

PriorityQueue Question

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381


I have two question.
(A) Why this program gives comilation warning?
(B) Output


~Sanjeev Singh<br />SCJP 1.5
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What does the warning message mean to you? Why not tell us the warning message you are getting?

To get the output you can run the program. Please show some effort to find the solution.


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

Joined: Nov 01, 2006
Posts: 381
While compilation
without -Xlint:unchecked option to javac
Note: C:\nbworkspace\SCJPKSBB\src\PriorityTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

with -Xlint:unchecked option to javac
C:\nbworkspace\SCJPKSBB\src\PriorityTest.java:21: warning: [unchecked] unchecked call to offer(E) as a member of the raw type java.util.Queue
q.offer(">Java<");
C:\nbworkspace\SCJPKSBB\src\PriorityTest.java:22: warning: [unchecked] unchecked call to offer(E) as a member of the raw type java.util.Queue
q.offer(">java<");
C:\nbworkspace\SCJPKSBB\src\PriorityTest.java:23: warning: [unchecked] unchecked call to offer(E) as a member of the raw type java.util.Queue
q.offer("> Java<");
C:\nbworkspace\SCJPKSBB\src\PriorityTest.java:24: warning: [unchecked] unchecked call to offer(E) as a member of the raw type java.util.Queue
q.offer("> java<");
C:\nbworkspace\SCJPKSBB\src\PriorityTest.java:25: warning: [unchecked] unchecked call to offer(E) as a member of the raw type java.util.Queue
q.offer(">$<");
5 warnings


I got these warnings.

Barry,I know the output but want the Ranchers to have a look and solve.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I don't quite understand, did you ask the question because you wanted to know the answer?


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
No I didn't mean that. I have got the answer for ( B ) but ( A ) is still unanswered.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Originally posted by Sanjeev Kumar Singh:
No I didn't mean that. I have got the answer for ( B ) but ( A ) is still unanswered.


Sanjeev,

You have to keep in mind that this site is also for learning. You have to show some effort that you are working towards the solution -- asking what is the output gives the impression that you never ran it yourself.


Anyway, the warning errors are caused by your use of generics. They are only warning errors and you may ignore them if you like... but to learn more, you may try the Sun tutorial on Generics.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Apart from Sun's tutorial there is also an entry in Angelika Langer's Generic FAQ
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Henry and Barry,
Thanks for the help.I will try to be more specific towards questioning next time.
[ November 23, 2006: Message edited by: Sanjeev Kumar Singh ]
B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
i have a doubt.i coplied the program i got the ouput as

(space)Java
(space)java
$
usually queue will sort it,so i am getting the above 3 output.but
i want to know what happenened to the other two entries
(without space)java and (without space)Java.
please let me know.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by B pandoo:
i have a doubt.i coplied the program i got the ouput as

(space)Java
(space)java
$
usually queue will sort it,so i am getting the above 3 output.but
i want to know what happenened to the other two entries
(without space)java and (without space)Java.
please let me know.


Yes, it's a tricky one - not fair really.

q.poll() removes the element from the queue. So think about the value of q.size() as you execute the loop. How many times does the loop execute if the size is changing.

When you have that sorted out, fix the program so that you get all five elements.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Sanjeev, knowing how to ask a question well is an art in itself. We dont mean to "wag our fingers" at you when we comment on the style of your asking. Don't be put off by our comments, keep posting and asking (and even answering) questions.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

To Sanjeev:
this warning occurs, when you use a generic class without parameterizing it.

To B pandoo:
perhaps try
System.out.println("index i:"+i + " size:"+q.size());
after the line with the poll, and have a look at the output.


Yours,
Bu.


all events occur in real time
B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
Thanks group.now i am clear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PriorityQueue Question