Sort elements of Java Vector - Java Collection Framework

Java examples for Collection Framework:Vector

Description

Sort elements of Java Vector

Demo Code


import java.util.Vector;
import java.util.Collections;
 
public class Main {
 
  public static void main(String[] args) {
   //from   ww  w .  j  av  a  2  s. c om
    //create Vector object
    Vector v = new Vector();
   
    //Add elements to Vector
    v.add("1");
    v.add("3");
    v.add("5");
    v.add("2");
    v.add("4");
 
    Collections.sort(v);
   
    //display elements of Vector
    System.out.println("Vector elements after sorting in ascending order : ");
    for(int i=0; i<v.size(); i++)
      System.out.println(v.get(i));
 
  }
}

Result


Related Tutorials