Interview Q&A · Java · 4 questions in this topic
ArrayList, Vector and Arrays — Java Interview Q&A
How resizable-array classes work, how they differ from plain arrays, and how ArrayList compares with legacy Vector. Part of Collections Framework · 294 questions in the Java bank.
What is ArrayList?
Answer
ArrayList is a resizable-array implementation of List that stores elements in a growable internal array. It gives fast constant-time random access by index and fast iteration, and it implements the RandomAccess marker interface. It allows duplicate elements and nulls, preserves insertion order, and is not synchronized so it must be synchronized externally for multithreaded use. When the backing array fills up, a larger array is allocated and the contents are copied over, which makes occasional adds slower but keeps average add cost low.
Example
List<String> names = new ArrayList<>(); names.add("Ada"); names.add("Grace"); String first = names.get(0);
Array vs ArrayList?
Answer
An array has a fixed length chosen at creation, while an ArrayList grows and shrinks dynamically as elements are added and removed. Arrays can hold primitives directly, while ArrayList holds objects only, so primitives are stored through autoboxing into wrapper types such as Integer. Arrays require manual logic and index management for insertion and removal, while ArrayList provides methods such as add, remove, get, set, and size. Arrays can be slightly faster for tiny fixed-size work, but ArrayList is usually clearer and backed by an array internally anyway.
Key points
- 1Fixed size versus dynamic resizing.
- 2Primitives allowed directly versus objects only with autoboxing for primitives.
- 3Manual index handling versus method-based add, remove, get, and set.
- 4Slight raw speed edge for arrays versus convenience and flexibility for ArrayList.
What is Vector?
Answer
Vector is a legacy resizable-array implementation of List dating from Java 1.0, before the Collections Framework existed. Every public method is synchronized, so it is thread safe for single operations but slower than ArrayList under single-threaded use. By default it doubles its capacity when it outgrows its array, whereas ArrayList grows by about half. The Stack class extends Vector, but modern code prefers ArrayList with explicit synchronization, or a Deque implementation such as ArrayDeque instead of Stack.
ArrayList vs Vector?
Answer
Both are resizable arrays with the same ordering and duplicate rules, but ArrayList is unsynchronized while Vector synchronizes every method. ArrayList is a modern Collections Framework class while Vector is a legacy class retrofitted to implement List. Their growth policies differ, with Vector doubling capacity and ArrayList growing by roughly fifty percent. Prefer ArrayList for performance and add explicit synchronization only when needed, reserving Vector for old APIs that require it.
Key points
- 1Thread safety: unsynchronized versus method-level synchronization.
- 2Age: modern versus legacy status.
- 3Growth: proportional growth versus doubling.
- 4Guidance: default to ArrayList, synchronize externally or use concurrent collections for threads.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission