Gadgets

Thursday, 21 January 2016

Explain about Vector with an example

1. Heterogeneous Objects are allowed.
2. Insertion order is preserved.
3. Duplicate Objects are allowed.
4. Null insertion is possible.
5. Underlying data structure is Resizable Array or Growable Array.
6. Vector class implements RandomAccess, Serializable and Cloneable interfaces.
7. Best choice for retrieval operation.
8. Every method present inside Vector is synchronized.
9. At a time one thread only allowed and it is thread safe.
10. Relatively performance wise is low because threads are required to wait. 
import java.util.*;
public class VectorDemo {
               public static void main(String[] arg) {                  
                               Vector v=new Vector();
                               v.add("a");
                               v.add(10);
                               v.add(null);//null insertion
                               v.add("a");//duplicate insertion
                               System.out.println(v);//[a, 10, null, a]
                               v.addElement("b");
                               v.removeElementAt(3);//remove element at v[3]
                               v.add(1,"n");//add v[1]=n
                               System.out.println(v);//[a, n, 10, null, b]
                               System.out.println(v.firstElement());//v[0]=a
                               System.out.println(v.lastElement());//v[4]=b
                               System.out.println(v.elementAt(1));//v[1]=n
               }              
}

No comments:

Post a Comment