Vector
implements List Interface. Like ArrayList
it also maintains insertion order but it is rarely used in non-thread environment as it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements.Example:
import java.util.*; public class VectorExample { public static void main(String args[]) { /* Vector of initial capacity(size) of 2 */ Vector<String> vec = new Vector<String>(2); /* Adding elements to a vector*/ vec.addElement("Apple"); vec.addElement("Orange"); vec.addElement("Mango"); vec.addElement("Fig"); /* check size and capacityIncrement*/ System.out.println("Size is: "+vec.size()); System.out.println("Default capacity increment is: "+vec.capacity()); vec.addElement("fruit1"); vec.addElement("fruit2"); vec.addElement("fruit3"); /*size and capacityIncrement after two insertions*/ System.out.println("Size after addition: "+vec.size()); System.out.println("Capacity after increment is: "+vec.capacity()); /*Display Vector elements*/ Enumeration en = vec.elements(); System.out.println("\nElements are:"); while(en.hasMoreElements()) System.out.print(en.nextElement() + " "); } }
Output:
Size is: 4 Default capacity increment is: 4 Size after addition: 7 Capacity after increment is: 8 Elements are: Apple Orange Mango Fig fruit1 fruit2 fruit3
No comments:
Post a Comment