Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Vector

  • • It is an implementation class of List interface.
  • • Underlying data structure is resizable or growable array.
  • • Duplicate objects are allowed.
  • • Insertion order is preserved.
  • • Null insertion is possible.
  • • Heterogeneous objects are allowed.
  • • Implements Serializable and Clonable interface.
  • • It also implements RandomAccess interface.
  • • Since it implements RandomAccess interface, performing retrieval operation, vector is the best choice.
  • • All the methods belonging to Vector are synchronized, that means the Vector object is thread safe, where as in ArrayList, the object is not in thread safe that means it is not synchronized.

The methods available in Vector class are:

addElement(Object o);
addElement(index);
removeElement(index);
removeElements();

Constructors:

Vector v=new Vector(Collection c);

Vector v=new Vector(int initial capacity);
Vector v=new Vector(1000);

Vector v=new Vector(int initial capacity, int incremental capacity);

Vector v=new Vector(Collection c);

  • • Creates an empty vector having the default initial capacity 10. If we want to insert a new object in the 11th position, then a new bigger vector will be created having new capacity: New capa city=2*current capacity Hence the new vector capacity will be 20.
  • • Create a vector having the given initial capacity. It have some disadvantage, that is suppose if we additionally want to insert five objects then the new vector will be created having double size of the current vector, after inserting 5 objects, the remaining spaces will be wasted.
  • • For exa: Vector v=new Vector(1000,5); This constructor increases the performance level.
  • • For every Collection c, we can create an equivalent Vector.
Example:
VectorDemo.java:
import java.util.*;
classVectorDemo
{
	public static void main(String args[])
	{
		Vector v=new Vector();
		System.out.println(v.capacity());
		for(inti=1;i<=10;i++)
		{
			v.addElement(i);
		}
		v.addElement("silan");
		System.out.println(v.capacity());
		System.out.println(v);
	}
}

Output:

10
20
[1,2,3,4,5,6,7,8,9,10,silan]

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext