The Vector Class
In Java, while you can give the size of an array at run time, you cannot dynamically change the size of an array during the computation. The vector class provides a data structure with just this property, but the restriction is that all of the elements must be of type Object.
- Usually, we insert an element of any type and Java will convert it to an Object, but when you extract an element, you must explicitly cast it to convert it back to the type you want.
A vector is created with an "initial capacity" and a "capacity increment". (The default is an initial capacity of 10 and an increment that doubles each time.) As you add elements, if the initial capacity is exceeded, then more memory is automatically allocated in the size of the capacity increment.
- Vector shoes = new Vector();
- Vector orders = new Vector(100, 10);