• 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

Generics

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am studying for SCJP 5.0 from SCJP book by Kathy Sierra and Bert Bates and had a question on auto boxing in generics


Below is the code followed by mu question



This code produces something like this:

1 3 5 6 7 8 9

size 7
peek 9
size 7
poll 9
size 6
8 7 6 5 3 1 null


HOW DOES THIS WORK?

1. return two - one


2.PQsort pqs = new PQsort(); // get a Comparator
PriorityQueue<Integer> pq2 =
new PriorityQueue<Integer>(10,pqs);


3. IN THE ABOVE CODE IF I CHANGE 10 to 100 I STILL GET THE SAME RESULT, WHY?

I APPRECIATE ALL YOUR HELP

Thank you
Sachin

( tags added)
[ October 04, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HOW DOES THIS WORK?

1. return two - one



compare method should return
Positive int : if first arg > then second arg.
0 or any negative : if first arg == to secong arg.
Negative int : if first arg < then second arg.


PQsort pqs = new PQsort(); // get a Comparator
PriorityQueue<Integer> pq2 =
new PriorityQueue<Integer>(10,pqs);


3. IN THE ABOVE CODE IF I CHANGE 10 to 100 I STILL GET THE SAME RESULT, WHY?




Why you expect results to be changed by changing 10 to 100....?
10 or 100 (any int in constructor) specifies initial capacity of PriorityQueue


Read the [B]javadoc if you have any doubt using method[/B]
[ October 04, 2006: Message edited by: Riyaz Saiyed ]
 
Riyaz Saiyed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typo
please read as,
compare method should return
Positive int : if first argument > then second argument.
0 (Zero) : if first argument == to second argument.
Negative int : if first argument< then second argument.
 
Sachin doshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Riyaz Saiyed
 
reply
    Bookmark Topic Watch Topic
  • New Topic