Vector - remove, get, contains and set methods program






Vector - remove, get, contains and set 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>();
          vector.add("ankit");
          vector.add("mittal");
          vector.add("javaMadeSoEasy");
          System.out.println("vector = "+vector);
         
         
          System.out.println("\n--------get element at 1st index i.e. mittal");
          System.out.println("element at 1st index = "+vector.get(1));
         
          System.out.println("\n--------contains element  ");
          System.out.println("vector.contains = "+vector.contains("mittal"));
         
          System.out.println("\n--------Remove element at 1st index i.e. mittal");
          vector.remove(1);
          System.out.println("vector = "+vector);
          System.out.println("\n---------Remove ankit from list");
          vector.remove("ankit");
          System.out.println("vector = "+vector);
          System.out.println("\n---------Set newVal at 0th index");
          vector.set(0,"newVal"); //javaMadeSoEasy will be replace by newVal
          System.out.println("vector = "+vector);
         
         
         
         
         
   }
}
/*OUTPUT
vector = [ankit, mittal, javaMadeSoEasy]
--------get element at 1st index i.e. mittal
element at 1st index = mittal
--------contains element
vector.contains = true
--------Remove element at 1st index i.e. mittal
vector = [ankit, javaMadeSoEasy]
---------Remove ankit from list
vector = [javaMadeSoEasy]
---------Set newVal at 0th index
vector = [newVal]
*/


RELATED LINKS>

Vector - add, add element at specific index 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



Must read for you :

No comments:

Post a Comment