Tuesday 26 May 2015

How to insert element in vector

import java.util.Vector;

public class AddElement {
   
    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 = "+vector);

        /*
         * Inserts the specified element at the specified
         * position in this Vector.
         */
        vector.add(1, "java");
               
        System.out.println("vector after insert element = "+vector);
    }
}

Output:

Vector = [cricket, hockey, football, tennish]

vector after insert element = [cricket, java, hockey, football, tennish]

No comments:

Post a Comment