Monday, 25 May 2015

How to remove all element from vector

We can remove all element of vector using clear() method . clear() removes all element from the list and return empty list.


import java.util.Vector;

public class RemoveAll {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector is = "+vector);
      
        /*
         * Removes all of the elements from this
         * Vector. The Vector will be empty after
         *  this call returns (unless it throws an
         *   exception).
         */
        vector.clear();
      
        System.out.println("Vector After Clear is = "+vector);
               
    }

}


Output:

Vector is = [cricket, hockey, football, tennish]

Vector After Clear is = []

No comments:

Post a Comment