Win a copy of Rust Web Development this week in the Other Languages 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

how to prevent vector to be with 10 elements as defult ?

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
I just notice in the debugger that when I create new Vector ()
it been initialize as as default with 10 elements how can I create
it with 0 elements as defult?
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the different constructors:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that a new vector does not really contain 10 elements - it only reserves space for 10 elements, so that adding data to the vector can be done more efficiently. The space for the 10 elements isn't used yet. When you create a new Vector with the default constructor and don't add any data to it, calling the method size() on the vector will return 0; not 10.

You really should not explicitly create a Vector with something else than the default number of elements unless you have a very specific reason to do this (for example, if you know beforehand that you're going to add 20 elements to the vector, you might let the vector reserve space for the 20 elements at the moment it is initialized).
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/**
* Constructs an empty vector so that its internal data array
* has size <tt>10</tt> and its standard capacity increment is
* zero.
*/
public Vector() {
this(10);
}


/**
* Constructs an empty vector with the specified initial capacity and
* capacity increment.
*
* @param initialCapacity the initial capacity of the vector.
* @param capacityIncrement the amount by which the capacity is
* increased when the vector overflows.
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}

/**
* Constructs an empty vector with the specified initial capacity and
* with its capacity increment equal to zero.
*
* @param initialCapacity the initial capacity of the vector.
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}

This will clarify !
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As this is the beginner forum, I will add the standard point that you should not be using Vector in any new code.

Vector is an old class, maintained for backwards compatibility only. You should use one of the newer implementations of List instead. The closest to Vector is ArrayList.

One reason for not using Vector is that it is internally synchronised. This may sound good initially, but in fact that synchronisation reduces performance in all applications, while very rarely achieving true thread safety in multi-threaded applications.

Same applies to Hashtable.

If an experienced Java hacker sees you using Vector or Hashtable in new code, they are likely to think badly of you, so don't do it!
 
a wee bit from the empire
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic