Vector is a type of data structure that maintains insertion order, that is elements are retrieved in same order as they are added into it. It is very much similar to ArrayList.
Vector Methods In JAVA:
Vector contains many legacy methods that are not part of collection framework which we will discuss below with examples in java.
1. boolean add(E e)
This method adds element at the end of the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObject.add(2); vectorObject.add(8); vectorObject.add(8); System.out.println("Values in Vector Object :" +vectorObject); } }
Output:
Values in Vector Object :[3, 5, 4, 1, 2, 8, 8]
2. void add(int index, E element)
This method adds an element at specified index and moves the whole elements one step in forward direction as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(0,3); vectorObject.add(1,5); vectorObject.add(2,4); vectorObject.add(3,1); for(Integer integer: vectorObject) { System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value: "+integer); } //Adding element at index 2 vectorObject.add(2, 10); System.out.println("\nAfter adding value at index 2:\n"); for(Integer integer: vectorObject) { System.out.println("Index : " +vectorObject.indexOf(integer)+ " Value: " +integer); } } }
Output:
Index : 0 Value: 3 Index : 1 Value: 5 Index : 2 Value: 4 Index : 3 Value: 1 After adding value at index 2: Index : 0 Value: 3 Index : 1 Value: 5 Index : 2 Value: 10 Index : 3 Value: 4 Index : 4 Value: 1
3. void addElement(E obj)
This method adds the specified element at the end of the vector also increasing its size by 1, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Values in Vecor object" +vectorObject); vectorObject.addElement(2); System.out.println("Values in Vecor object" +vectorObject); } }
Output:
Values in Vecor object[3, 5, 4, 1] Values in Vecor object[3, 5, 4, 1, 2]
4. int capacity()
This method gives the capacity of the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("current capacity of Vector object is " +vectorObject.capacity()); } }
Output:
current capacity of Vector object is 4
5. void clear()
This method clears all the elements in the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Values in Vector object are " +vectorObject); vectorObject.clear(); System.out.println("Values in Vector object are " +vectorObject); } }
Output:
Values in Vector object are [3, 5, 4, 1] Values in Vector object are []
6. clone clone()
This method gives a duplicate copy of the whole vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Vector<Integer> vectorObjectclone = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObjectclone=(Vector<Integer>) vectorObject.clone(); System.out.println("Values in Vector object are " +vectorObjectclone); } }
Output:
Values in Vector object are [3, 5, 4, 1]
7. boolean contains(Object o)
This method tells whether the vector contains the specified element in the vector, it will return true if that element is present or false if it is not present, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("vector object contains elemnent 4?" +vectorObject.contains(4)); System.out.println("vector object contains elemnent 10?" +vectorObject.contains(10)); } }
Output:
vector object contains elemnent 4? true vector object contains elemnent 10? false
8. boolean containsAll(Collection c)
This method returns true if all the elements of one collection is present in calling collection, or false if not, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Vector<Integer> vectorObject1 = new Vector<Integer>(4); Vector<Integer> vectorObject2 = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObject1.add(3); vectorObject1.add(5); vectorObject1.add(4); vectorObject1.add(1); vectorObject2.add(4); vectorObject2.add(5); vectorObject2.add(4); vectorObject2.add(1); System.out.println("Verfing elements in object and object1 " +vectorObject1.containsAll(vectorObject)); System.out.println("Verfing elements in object and object2 " +vectorObject2.containsAll(vectorObject)); } }
Output:
Verfing elements in object and object1 true Verfing elements in object and object2 false
9. void copyInto(Object anArray)
This method copies all the elements vector to the array specified array in argument, It overlaps all the values of same index values, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Integer test[]= new Integer[6]; vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); test[0]=10; test[1]=11; test[2]=12; test[3]=13; test[4]=14; test[5]=15; vectorObject.copyInto(test); for(int i=0;i<test.length;i++) { System.out.println(test[i]); } } }
Output:
3 5 4 1 14 15
10. Object elementAt(int index)
This method gives specific element at index mentioned in the argument, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Element at position 2 : " +vectorObject.elementAt(2) ); } }
Output:
Element at position 2 : 4
11. Enumeration elements()
This method returns an object of Enumeration class that can be used for traversing over elements of vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); Enumeration enumobj=vectorObject.elements(); System.out.println("Data in enum object :"); while (enumobj.hasMoreElements()) { System.out.println("value: " + enumobj.nextElement()); } } }
Output:
Data in enum object : value: 3 value: 5 value: 4 value: 1
12. boolean equals(Object o)
This method checks the equality of object with this vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Vector<Integer> vectorObject1 = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); vectorObject1.add(3); vectorObject1.add(5); vectorObject1.add(4); vectorObject1.add(1); System.out.println("Testing two vector objects: " +vectorObject.equals(vectorObject1)); } }
Output:
Testing two vector objects: true
13. Object firstElement()
This method returns the first element at index 0, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("value at first index " +vectorObject.firstElement()); } }
Output:
value at first index 3
14. Object get(int index)
This method retrieves an element at specified index, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("value at index 2: " +vectorObject.get(2)); } }
Output:
value at index 2: 4
15. int hashCode()
This method retrieves an hash code value of given vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Hash code of Vector is : " +vectorObject.hashCode()); } }
Output:
Hash code of Vector is : 1017824
16. int indexOf(Object o)
This method retrieves the index of the first occurrence of the specified element in the vector, or -1 if that element is not present as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Index of element 1 is : " +vectorObject.indexOf(1)); } }
Output:
Index of element 1 is : 3
17. int indexOf(Object o, int index)
This method retrieves an index of the first occurrence of the specified element in the vector, searching forwards from index specified in the method or it returns -1 if the element is not found as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(5); vectorObject.add(1); vectorObject.add(5); vectorObject.add(5); System.out.println("Index of element 5 after index 2 is : " +vectorObject.indexOf(5, 2)); } }
Output:
Index of element 5 after index 2 is : 3
18. void insertElementAt(Object o, int index)
This method inserts the specified element at specified position and moves all the elements one step further as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Values in Vector object are :" +vectorObject); vectorObject.insertElementAt(12, 1); System.out.println("Values in Vector object are :" +vectorObject); } }
Output:
Values in Vector object are :[3, 5, 4, 1] Values in Vector object are :[3, 12, 5, 4, 1]
19. boolean isEmpty()
This method checks weather vector is empty or not as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Is vector object empty: " +vectorObject.isEmpty()); vectorObject.clear(); System.out.println("Is vector object empty :" +vectorObject.isEmpty()); } }
Output:
Is vector object empty: false Is vector object empty :true
20. Object lastElement()
This method returns the last element of the vector as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(4); vectorObject.add(1); System.out.println("Last element of vector object is : " +vectorObject.lastElement()); } }
Output:
Last element of vector object is : 1
21. int lastIndexOf(Object o)
This method retrieves the index of the last occurrence of the specified element in the vector, or -1 if that element is not present as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(1); vectorObject.add(3); System.out.println("Last occurance of element 3 is : " +vectorObject.lastIndexOf(3)); } }
Output:
Last occurance of element 3 is : 5
22. Object remove(int index)
This method removes an element at specified index, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(3); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); System.out.println("Values in vector :" +vectorObject); System.out.println("removed element at index 2 : " +vectorObject.remove(2)); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[3, 5, 3, 4] removed element at index 2 : 3 Values in vector :[3, 5, 4]
23. boolean remove(Object o)
This method removes the first occurrence of the specified element in the Vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); System.out.println("remove first occouranc of element 3 : " +vectorObject.remove((Integer)3)); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] remove first occouranc of element 3 : true Values in vector :[5, 4, 3]
24. boolean removeAll(Collection c)
This method removes all the elements of specified collection form the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Vector<Integer> vectorObject1 = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject1.add(4); vectorObject1.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.removeAll(vectorObject1); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4] Values in vector :[5]
25. void removeAllElements()
This method removes all the elements of the vector, and set its size to 0 as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.removeAllElements(); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] Values in vector :[]
26. void removeElementAt(int index)
This method removes an element at specified index, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.removeElementAt(1); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] Values in vector :[5, 4, 3]
27. protected void removeRange(int fromIndex, int toIndex)
This method removes range of elements between fromIndex(inclusive) and toIndex(exclusive), as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); vectorObject.add(1); vectorObject.add(7); vectorObject.add(8); System.out.println("Values in vector :" +vectorObject); System.out.println("Remove elements from 3rd to 5th index"); vectorObject.subList(3,5).clear(); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3, 1, 7, 8] Remove elements from 3rd to 5th index Values in vector :[5, 3, 4, 7, 8]
28. boolean retainAll(Collection c)
This method retains only those elements in the Vector that are contained in the specified Collection in the argument, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Vector<Integer> vectorObject1 = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); vectorObject.add(1); vectorObject.add(7); vectorObject.add(8); vectorObject1.add(3); vectorObject1.add(1); vectorObject1.add(7); System.out.println("Values in vector :" +vectorObject); vectorObject.retainAll(vectorObject1); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3, 1, 7, 8] Values in vector :[3, 3, 1, 7]
29. Object set(int index, Object element)
This method replaces the element with the element specified in the method at the specified index, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.set(1, 15); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] Values in vector :[5, 15, 4, 3]
30. void setElementAt(Object o, int index)
This method similar to set method replaces the element with the element specified in the method at the specified index, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.setElementAt(15,1); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] Values in vector :[5, 15, 4, 3]
31. void setSize(int newSize)
This method sets the size of this vector with the new size as specified in the argument, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Values in vector :" +vectorObject); vectorObject.setSize(10); System.out.println("Values in vector :" +vectorObject); } }
Output:
Values in vector :[5, 3, 4, 3] Values in vector :[5, 3, 4, 3, null, null, null, null, null, null]
32. int size()
This method gives the number of elements in the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Size of the vector is : " +vectorObject.size()); } }
Output:
Size of the vector is : 4
33. List subList(int fromIndex, int toIndex)
This method returns group of elements between fromIndex(inclusive) and toIndex(exclusive) as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("sub List Retrieved is : " +vectorObject.subList(2, 7)); } }
Output:
sub List Retrieved is : [4, 3, 4, 3, 4]
34. object toArray()
This method returns an array of elements in the same order as in the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); Object[] test= new Integer[4]; vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); test= vectorObject.toArray(); for(int i=0;i<test.length;i++) { System.out.println("Value: " +test[i]); } } }
Output:
Value: 5 Value: 3 Value: 4 Value: 3
35. String toString()
This method returns a string representation of the Vector, containing all the elements in string form, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(4); String test=""; vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); test=vectorObject.toString(); System.out.println("String form of Vector Object: "+ test); } }
Output:
String form of Vector Object: [5, 3, 4, 3]
36. void trimToSize()
This method makes the capacity of vector equal to the number of elements in the vector, as shown in the following program:
import java.util.*; public class VectorMethodsDemo { public static void main(String args[]){ Vector<Integer> vectorObject = new Vector<Integer>(10); String test=""; vectorObject.add(5); vectorObject.add(3); vectorObject.add(4); vectorObject.add(3); System.out.println("Size of Vector object : "+ vectorObject.capacity()); vectorObject.trimToSize(); System.out.println("Size of Vector object : "+ vectorObject.capacity()); } }
Output:
Size of Vector object : 10 Size of Vector object : 4