Monday 25 May 2015

Reverse order of all elements of Vector

import java.util.Collections;
import java.util.Vector;

public class ReverseAllelement {

    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("Before reverse Vector = "+vector);
      
        /*
         * Reverses the order of the elements in the
         * specified list. This method runs in linear time.
         */
        Collections.reverse(vector);
      
      System.out.println("After reverse Vector = "+vector);
               
    }
}


Output:

Before reverse Vector = [cricket, hockey, football, tennish]

After reverse Vector = [tennish, football, hockey, cricket]

No comments:

Post a Comment