Interview Q&A · Java · 4 questions in this topic

ArrayList, Vector and Arrays — Java Interview Q&A

All Java topics

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

Example.javajava
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

  1. 1Fixed size versus dynamic resizing.
  2. 2Primitives allowed directly versus objects only with autoboxing for primitives.
  3. 3Manual index handling versus method-based add, remove, get, and set.
  4. 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.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits