- The Vector class implements a growable array of objects. Similar to array.
- It is similar to ArrayList, but with two differences:
- Vector is synchronized.
- Vector contains many legacy methods that are not part of the collections framework.
- The elements of Vector can be accessed using an integer index.
- The size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
- Vector is synchronized which means it is suitable for thread-safe operations
Vector
implements List Interface. LikeArrayList
it also maintains insertion order- it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements.
class Vector <T>
We can create Vector for storing String type elements as follows:
Vector <String> hs=new Vector <String> ();
We can create Vector for storing Integer type elements as follows:
Vector <Integer> hs=new Vector <Integer> ();
Constructors of Vector class as show below:
- Vector( ): This constructor creates a default vector, which has an initial size of 10
- Vector(int size): This constructor is use to create vector of specific size .They are accept an argument that equals to the required size.
- Vector(int size, int incr): This constructor creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward
- Vector(Collection c) :creates a vector that contains the elements of collection c
Methods of Vector Class:
- void addElement(Object element): It inserts the element at the end of the Vector.
- int capacity(): This method returns the current capacity of the vector.
- int size(): It returns the current size of the vector.
- void setSize(int size): It changes the existing size with the specified size.
- boolean contains(Object element): This method checks whether the specified element is present in the Vector. If the element is been found it returns true else false.
- boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
- Object elementAt(int index): It returns the element present at the specified location in Vector.
- Object firstElement(): It is used for getting the first element of the vector.
- Object lastElement(): Returns the last element of the array.
- Object get(int index): Returns the element at the specified index.
- boolean isEmpty(): This method returns true if Vector doesn’t have any element.
- boolean removeElement(Object element): Removes the specifed element from vector.
- boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
- void setElementAt(Object element, int index): It updates the element of specifed index with the given element.
Example of Vector in Java:
import
java.util.Iterator;
import
java.util.Vector;
public
class
MyVectorDemo {
public
static
void
main(String a[]){
Vector<String> vct =
new
Vector<String>()
//adding elements to the end
vct.add(
"A"
);
vct.add(
"B"
);
vct.add(
"C"
);
vct.add(
"D"
);
Iterator<String> itr = vct.iterator();
while
(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
A
B
C
D
Another example of Vector in Java:
import
java.util.Vector;
public
class
MyVectorOperations {
public
static
void
main(String a[]){
Vector<String> vct =
new
Vector<String>();
//adding elements to the end
vct.add(
"A"
);
vct.add(
"B"
);
vct.add(
"C"
);
System.out.println(vct);
//adding element at specified index
vct.add(
2
,
"D"
);
System.out.println(vct);
//getting elements by index
System.out.println(
"Element at index 3 is: "
+vct.get(
3
));
//getting first element
System.out.println(
"The first element of this vector is: "
+vct.firstElement());
//getting last element
System.out.println(
"The last element of this vector is: "
+vct.lastElement());
//check vector is empty or not
System.out.println(
" vector empty? "
+vct.isEmpty());
}
}
Output:
[A, B, C]
[A, B, D, C]
Element at index 3 is: C
The first element of this vector is: A
The last element of this vector is: C
vector empty? false
[…] Vector is synchronized which means multiple threads can’t work on ArrayList at the same […]
LikeLike
Nice
LikeLike