• 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

Vectors

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers :
Vector v = new Vector(1);
for(int i = 0; i<20; i++)
v.add(new Integer(i));
System.out.println(v.size());
System.out.println(v.capacity());
Ans: 20
32
How come the capacity is 32 , when the increment is 0??
If i do Vector v = new Vector(1,1), its works fine
Q2: Whats the critical difference between add() and addElement()

Ragu
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how you understand this kinda behavior:

Now, you tell us what's going on.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's one of the best replies I've seen!! I've heard that a wise man answers a question with another question and now I'm seeing it in action!
Sasikanth
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q2: Whats the critical difference between add() and addElement()

addElement:

"This method is identical in functionality to the add(Object) method (which is part of the List interface)."

Reference

[This message has been edited by Marilyn deQueiroz (edited October 16, 2001).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
[B]Here's how you understand this kinda behavior:

Now, you tell us what's going on.
[/B]


Mike
Thankyou very much for response
You do make sure that we understand thoroughly
I appreciate that very much
From this code what i understood was
Whenever the size=capacity,
the vectors doubles its capacity

Please correct me if i am wrong
thankx
Ragu
PS: Why there is a special method addElement(Obj) for Vector?
It seems to do the same work as add(Obj) from list?
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are of course right, Ragu. As an bonus question: how do you change the behavior so capacity doesn't double every time the Vector fills up?
Marilyn's comments on add() and addElement() are correct: add() satisfies the List interface requirement, so that Vector can implement List. There's no need for them to be different, but if you took out addElement() because it's redundant, you might break someone's code. That's what happens when you need to redesign your interface...
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use another constructor to create Vector object as:
Vector(int initialCapacity, int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.
Vector v = new Vector(1, 10);
it will icrease the vector capacity in increment of 10.
--Farooq
reply
    Bookmark Topic Watch Topic
  • New Topic