![]() |
VOOZH | about |
Vector and ArrayList are dynamic array implementations in Java used to store elements that can grow or shrink in size. Both are part of the java.util package and provide indexed access to elements.
ArrayList is a resizable array implementation of the List interface that allows dynamic storage of elements. It is widely used because it provides fast random access and flexible size management.
Syntax:
List<DataType> listName = new ArrayList<>();
Names: [Ravi, Aman, Neha, Priya] Element at index 2: Neha After removal: [Ravi, Neha, Priya] Total Elements: 3
Vector is a dynamic array implementation in Java that grows automatically when elements are added. It is similar to ArrayList but it is synchronized and thread-safe.
Syntax:
Vector<DataType> vectorName = new Vector<>();
Names: [Ravi, Aman, Neha, Priya] Element at index 1: Aman After removal: [Ravi, Neha, Priya] Total Elements: 3
| Feature | ArrayList | Vector |
|---|---|---|
| Introduction | Introduced in Java 1.2 (Collection Framework) | Legacy class (Java 1.0) |
| Package | java.util | java.util |
| Synchronization | Not synchronized | Synchronized (thread-safe) |
| Performance | Faster (no synchronization overhead) | Slower due to synchronization |
| Growth Mechanism | Increases size by 50% when full | Doubles size when full (by default) |
| Thread Safety | Not thread-safe | Thread-safe |
| Preferred Use | Single-threaded applications | Multi-threaded applications (rarely used today) |
| Traversal | Iterator | Iterator + Enumeration (legacy support) |