Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Vector initialization

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following will successfully create an instance of the Vector class and add an element?

1) Vector v=new Vector(99);
v[1]=99;


2) Vector v=new Vector();
v.addElement(99);


3) Vector v=new Vector();
v.add(99);


4 Vector v=new Vector(100);
v.addElement("99");

Why is the answer to this is option (4) and not option (3)?

Please help.
Lovleen.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option 4 is the only one that adds an Object. All other options are adding a primitive integer. Collections can only have objects, so primitives need to be wrapped in an object,
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steven Young:
Option 4 is the only one that adds an Object. All other options are adding a primitive integer. Collections can only have objects, so primitives need to be wrapped in an object,



Right..But due to autoboxing feature in SCJP 5.0 .. will option 3 not be valid as well?
 
Steven Young
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I thought it was an SCJP 1.4 question.

Otherwise, using Java 5 compiler, option 2, 3 & 4 is OK. Only get warnings because the Vector is not type-safe.

You sure it is not a question designed for Java 1.4 ??
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loveleen You are correct!


Option 2, 3 and 4 are correct!

Autoboxing feature makes option 4 correct. ***

btw, what is source of this erroneous question?


Regards,
cmbhatt


***Only applicable from Java 5.0
[ April 12, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
addElement is a method only of Vector class, not of List.
I've not understood the difference between the two methods add - addElement. Is it just the return type (boolean for add(E o), void for addElement(E obj)).
When is it advisable to use addElement?
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic