• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Vector Constructors

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! A recent thread alerted me about this question.

from the Sun Java documentation

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

there are several constructors for vector. I'm primarily confused about these ones...

Vector(int initialCapacity)
Vector(int initialCapacity, int capacityIncrement)

why is it that when I do

Vector v = new Vector(100);
System.out.println("Size: " + v.size() );

I get an output of zero? Thanks!
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just been looking into Vectors myself.

I think Vector constructor that allows you to specify the initial capacity and capacity increment is for performance purposes. If you know you are going to add lots of elements, you can specify a large initial capacity so the underlying JVM doesn't have to spend lots of time resizing the growing Vector object (this is my theory anyway).

The reason why the size is zero after you have constructed the Vector object using an initial capacity of 100 is because the size() method gives you a count of the number of elements in the Vector. Since you haven't actually added any elements, size is zero. You have only created a Vector object that is capable of storing 100 elements before the JVM needs to resize the object to fit more elements.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The vector is capable of holding up to 100 but since you haven't added any elements, it is zero. Size() returns the number of elements in the vector.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The initial capacity, which refers to the number of elements the Vector can hold before it has to grow. If a Vector requires additional capacity, it grows by a capacity increment that you specify. If you do not specify a capacity increment, the system will double the size of a Vector each time additional capacity is needed.

Size basically Returns the number of components in this vector.

Hope this helps!!!
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remember, Vectors aren't on the 5.0 exam., and they're barely on the 1.4.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That's one thing less to memorize. Thanks all for your efforts to answer!
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic