Vectors in Java
๐งญ Vectors in Java
In Java, when we need to store a dynamic list of elements (that can grow or shrink during runtime), we use the Vector class.
Before modern collections like ArrayList
were introduced, Vector
was widely used to store dynamic data. Even today, it remains important for understanding the evolution of Java’s Collection Framework.
๐น What is a Vector?
A Vector in Java is a resizable array that can store elements of the same type.
Unlike regular arrays that have a fixed size, vectors automatically increase their capacity when more elements are added.
Vectors belong to the package:
๐น Key Features of Vectors
✅ Can grow or shrink dynamically
✅ Can store duplicate elements
✅ Maintains insertion order
✅ Synchronized — thread-safe (unlike ArrayList
)
✅ Provides many useful built-in methods for easy manipulation
๐น Syntax to Create a Vector
Example:
๐น Adding Elements to a Vector
You can use the add()
method to insert elements.
Output:
๐น Accessing Elements in a Vector
You can access elements using the get(index)
method or an iterator.
Or using a loop:
๐น Updating and Removing Elements
Update an element:
Output:
Remove an element:
Output:
๐น Example 1: Basic Vector Operations
Output:
๐น Iterating Through a Vector
You can traverse a Vector using:
1️⃣ For Loop
2️⃣ Enhanced For Loop (For-Each)
3️⃣ Iterator
๐น Vector Methods at a Glance
Method | Description | Example |
---|---|---|
add(element) | Adds element at the end | v.add(10) |
add(index, element) | Inserts element at specific position | v.add(1, 25) |
get(index) | Returns element at index | v.get(2) |
set(index, element) | Replaces element | v.set(0, 99) |
remove(index) | Removes element at index | v.remove(1) |
clear() | Removes all elements | v.clear() |
size() | Returns number of elements | v.size() |
contains(value) | Checks if element exists | v.contains(30) |
isEmpty() | Checks if vector is empty | v.isEmpty() |
firstElement() | Returns first element | v.firstElement() |
lastElement() | Returns last element | v.lastElement() |
capacity() | Returns current capacity | v.capacity() |
๐น Example 2: Using Vector Methods
Output:
(By default, a Vector starts with a capacity of 10 and doubles when full.)
๐น Vector vs ArrayList
Feature | Vector | ArrayList |
---|---|---|
Synchronization | ✅ Synchronized (Thread-safe) | ❌ Not synchronized |
Performance | ⚙️ Slower (because of synchronization) | ⚡ Faster |
Growth | Doubles its size | Increases by 50% |
Introduced in | JDK 1.0 (Legacy class) | JDK 1.2 |
Use Case | Multi-threaded programs | Single-threaded programs |
๐น Example 3: Copy All Elements from One Vector to Another
Output:
๐น Example 4: Removing All Elements from a Vector
Output:
๐ก Summary
Concept | Description |
---|---|
Vector | Dynamic array from java.util package |
Resizable | Grows automatically as elements are added |
Thread-safe | Synchronized, safe for multi-threading |
Common methods | add() , get() , set() , remove() , size() , capacity() |
Alternative | ArrayList — faster but not synchronized |
๐งพ Key Takeaway
Vectors are an important part of Java’s legacy collection classes.
Even though ArrayList
is more commonly used today, understanding Vector
helps build a solid foundation for Java Collection Framework concepts like synchronization and dynamic storage.
Example Programs
1️⃣ Program to Store and Print 5 Student Names using a Vector
Output:
2️⃣ Program to Remove Duplicate Elements from a Vector
Output:
3️⃣ Create a Vector of Integers and Find the Largest Element
Output:
4️⃣ Program to Copy All Elements from One Vector to Another
Output:
5️⃣ Implement a Vector of Strings and Search for a Given Word
Example Output:
Comments
Post a Comment