Vector:
Vector in Java:
Vector:
Vector can be created using:Vector<String> vector = new Vector<>();
Vector is created without specifying capacity, its
default capacity is 10.
Vector is 0 (no elements added yet).
Size = 0, Capacity = 10
Vector:
add() method:vector.add(10);vector.add(20);vector.add(30);vector.add(40);
Vector automatically
increases its capacity.
newCapacity = oldCapacity * 2 (i.e., it doubles its size).
get(int index) and updated using set(int index, E element).
System.out.println(vector.get(1)); // prints 20
vector.set(1, 222);
System.out.println(vector); // prints [10, 222, 30, 40]
Vector are synchronized, meaning only one thread can access it at a time.
ArrayList.
Vector<Integer> v = new Vector<>();
v.add(10);
v.add(20);
Vector:
Iterator, ListIterator,
Enumeration, or an enhanced for-each loop.
Enumeration<String> en = vector.elements();
while (en.hasMoreElements())
{
System.out.println(en.nextElement());
}
Vector.
Vector is Good for:
Vector are synchronized, making it thread-safe.
get(int index)).
ArrayList, improving cache locality.
Vector automatically increases its capacity by 100% (doubles it).
Vector is Not Good for:
ArrayList performs better in such scenarios.
ArrayList and CopyOnWriteArrayList.
| Operation | Performance | Complexity |
|---|---|---|
Access by index (get) |
Fast | O(1) |
Update by index (set) |
Fast | O(1) |
Add at end |
Fast (amortized) | O(1) |
Insert/Delete in middle |
Slow (due to shifting) | O(n) |
Remove by value |
Slow | O(n) |
Search (contains) |
Moderate | O(n) |
Iteration |
Fast | O(n) |
Thread Safety |
Yes (synchronized) | Overhead present |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.