aspose file tools*
The moose likes Beginning Java and the fly likes how to prevent vector to be with 10 elements as defult ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to prevent vector to be with 10 elements as defult ?" Watch "how to prevent vector to be with 10 elements as defult ?" New topic
Author

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

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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?
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Check out the different constructors:

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


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

/**
* 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 !
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to prevent vector to be with 10 elements as defult ?