Vector in Java

Java Vectors are commonly used instead of arrays, because they expand automatically when new data is added to them. That means the Vector instances, like linked-lists, can grow dynamically . However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
//Create a Vector with default initial size Vector vcTr = new Vector();
//Create a Vector with an initial size Vector vcTr = new Vector(300);
How to Java Vector A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. Each Vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement . An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
Vector(int initialCapacity, int capacityIncrement);
Vector vs Arraylist The Vector class is thread-safe. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time. ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. So you won't get a ConcurrentModificationException. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

The following Java program illustrates several of the methods supported by this Vector collection Framework:

import java.util.*; class TestClass { public static void main (String[] args) throws java.lang.Exception { //create new Vector Object Vector vcTr = new Vector(); System.out.println("Vector Size: " + vcTr.size()); // initial size is 2, increment is 2 vcTr = new Vector(2,2); System.out.println("Vector Size: " + vcTr.size()); vcTr.addElement("Sunday"); vcTr.addElement("Monday"); vcTr.addElement("Wednesday"); System.out.println("Vector Size: " + vcTr.size()); System.out.println("Vector Capacity: " + vcTr.capacity()); //you can see here the capacity doubled as 4 //using Iterator to see all elemnets in vector Iterator<String> itr = vcTr.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } //adding element at specified index vcTr.add(2,"Tuesday"); //using Enumeration to list all elemnts in a Vector Enumeration<String> enm = vcTr.elements(); while(enm.hasMoreElements()){ System.out.println(enm.nextElement()); } //Display first element from Vector System.out.println(vcTr.firstElement()); //Display last element from vector System.out.println(vcTr.lastElement()); //Retrieve vector elements by index System.out.println(vcTr.get(2)); //Search an item in Vector if(vcTr.contains("Monday")) System.out.println("Item Found"); //Remove a single element from Vector vcTr.remove(1); System.out.println(vcTr); //Remove all elements from vector vcTr.clear(); //Check Vector is empty or not if(vcTr.isEmpty()) System.out.println("Vector Empty !!"); else System.out.println("Vector Contains: " + vcTr.size() + " elements !!"); } }