Vector Class


Vector Class


  • The Vector class implements a growable array of objects. 
  • It is fully compatible with collections.
  • Vector implements a dynamic array.
  • Like an array, it contains components that can be accessed using an integer index
  • We can import from Java.util.Vector.


Constructor:

  • Vector(): Creates a default vector of initial capacity is 10.
  • Vector(int size): Creates a vector whose initial capacity is specified by size.
  • Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It 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 in Vector:


  • boolean add(Object obj): This method appends the specified element to the end of this vector.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(3);
System.out.println("Vector is " + v);


  • void add(int index, Object obj): This method inserts the specified element at the specified position in this Vector.
  • Example:

Vector v = new Vector();
v.add(0,1);
v.add(1,2);
v.add(2,"Sarthak");
v.add(3,"Patel");
v.add(4,3);
System.out.println("Vector is " + v);


  • boolean addAll(Collection c) This method appends all of the elements in the specified Collection to the end of this Vector.
  • Example:

ArrayList arr = new ArrayList();
arr.add(3);
arr.add("Sarthak");
arr.add("Patel");
arr.add(4);
Vector v = new Vector();
v.addAll(arr);
System.out.println("vector v:" + v);


  • boolean addAll(int index, Collection c) This method inserts all of the elements in the specified Collection into this Vector at the specified position.
  • Example:

ArrayList arr = new ArrayList();
arr.add(3);
arr.add("Sarthak");
arr.add("Patel");
arr.add(4);
Vector v = new Vector();
v.add(2);
v.addAll(1,arr);
System.out.println("vector v:" + v);


  • void clear() This method removes all of the elements from this vector.
  • Example:

v.clear();
System.out.println("after clearing: " + v);


  • Object clone() This method returns a clone of this vector.
  • Example:

Vector v = new Vector();
Vector v_clone = new Vector();
v.add(0, 1);
v.add(1, 2);
v.add(2, "Sarthak");
v.add(3, "Patel");
v.add(4, 3);
v_clone = (Vector)v.clone();
System.out.println("Clone of v: " + v_clone);


  • boolean contains(Object o): This method returns true if this vector contains the specified element.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(3);
if (v.contains("Sarthak"))
    System.out.println("It is present.");


  • void ensureCapacity(int minCapacity): This method increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument .
  • Example:

Vector v = new Vector();
v.ensureCapacity(22);
System.out.println("Minimum capacity: " + v.capacity());


  • Object get(int index):This method returns the element at the specified position in this Vector.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(4);
System.out.println("element at index 2 is: " + v.get(2));


  • boolean isEmpty(): This method tests if this vector has no components.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(4);
v.clear();
if (v.isEmpty())
    System.out.println("Vector is clear");


  • boolean remove(Object o): This method removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(4);
v.remove(1);
System.out.println("after removal: " + v);


  • Object firstElement(): This method returns the first component (the item at index 0) of this vector.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(4);
System.out.println("first element of vector is: " + v.firstElement());


  • int size(): This method returns the number of components in this vector.
  • Example:

Vector v = new Vector();
v.add(1);
v.add(2);
v.add("Sarthak");
v.add("Patel");
v.add(4);
System.out.println(" size of vector: " + v.size());

ArrayList Vs. Vector

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form