The Vector class provides the ability to implement a scalable array, and the array becomes larger as more elements are added. After you delete some elements, the array becomes smaller.
The Vector has three constructors:
Public Vector (int initialcapacity,int capacityincrement)
Public Vector (int initialcapacity)
Public Vector ()
The Vector runtime creates an initial storage capacity initialcapacity, and the storage capacity is incremental growth defined by the capacityincrement variable. The initial storage capacity and capacityincrement can be defined in the vector's constructor. The second constructor creates only the initial storage capacity. The third constructor does not specify neither the initial storage capacity nor the capacityincrement.
The vector class provides access methods that support similar array operations and operations associated with vector size. An array-like operation allows the addition of vectors, deleting and inserting elements. They also allow you to test the contents of the vector and retrieve the specified elements, and size-related operations allow you to determine the byte size and the number of elements in the vector.
Now for the frequent use of the vector increase, delete, interpolation function examples described:
AddElement (Object obj)
Add the component to the tail of the vector with a size plus 1, and the vector capacity is 1 larger than before
Insertelementat (Object obj, int index)
Add the component to the set index, and then move the contents back 1 units
Setelementat (Object obj, int index)
Add the component to the set index, where the content is substituted.
Removeelement (Object obj) removes the contents of this component from the vector.
Removeallelements () Removes all components in the vector, with a vector size of 0.
For example:
import java.lang.System;
import java.util.Vector;
import java.util.Emumeration;
public class Avector{
public static void main(String args[]){
Vector v=new Vector();
v.addElement("one");
v.addElement("two");
v.addElement("three");
v.insertElementAt("zero",0);
v.insertElementAt("oop",3);
v.setElementAt("three",3);
v.setElementAt("four",4);
v.removeAllElements();
}
}
Changes in vectors:
1. One 2. One 3. One 4. Zero 5.zero 6. Zero 7. Zero
8.
Two two one one one one
Three two two two two
Three OOP three three
Three three Four