Vector - add, add element at specific index methods program








Vector - add, add element at specific index methods program.



import java.util.Vector;
import java.util.List;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class VectorTest {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          List<String> vector = new Vector<String>();
          System.out.println("--------add element to end of list");
          vector.add("ankit");
          vector.add("mittal");
          vector.add("javaMadeSoEasy");
          System.out.println("vector = "+vector);
          System.out.println("--------add element at 1st index i.e. after ankit");
          vector.add(1,"java");
          System.out.println("vector = "+vector);
         
   }
}
/*OUTPUT
--------add element to end of list
vector = [ankit, mittal, javaMadeSoEasy]
--------add element at 1st index i.e. after ankit
vector = [ankit, java, mittal, javaMadeSoEasy]
*/


RELATED LINKS>


Vector - remove, get, contains and set methods program


Vector - iterate using iterator, listIterator, Enumeration and enhanced for loop


Vector - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop



No comments:

Post a Comment