In this section, you will learn how to use the java.util.vector class.In this section, you will learn how to use the java.util.vector class.
In this example we are going to show the use of java.util.Vector class. We will be creating an object of Vector class and performs various operation like adding, removing etc. Vector class extends AbstractList and implements List, RandomAccess, Cloneable, Serializable. The size of a vector increase and decrease according to the program. Vector is synchronized.
In this example we are using seven methods of a Vector class.
add(Object o): It adds the element in the end of the Vector
size(): It gives the number of element in the vector.
elementAt(int index): It returns the element at the specified index.
firstElement(): It returns the first element of the vector.
lastElement(): It returns last element.
removeElementAt(int index): It deletes the element from the given index.
elements(): It returns an enumeration of the element
In this example we have also used Enumeration interface to retrieve the value of a vector. Enumeration interface has two methods.
hasMoreElements(): It checks if this enumeration contains more elements or not.
nextElement(): It checks the next element of the enumeration.
Code of this program is given below:
Output of this example is given below:
|C:\Java Tutorial>javac VectorDemo.java
C:\Java Tutorial>java VectorDemo
the elements of vector: [10, 20, 30, tapan joshi]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: tapan joshi
The elements of vector: [10, 20, tapan joshi]
The elements are: 10
The elements are: 20
The elements are: tapan joshi