ArrayList<E> is a resizable array implementation of List—the default dynamic list in Java. Import from java.util.
Common operations
ArrayList<String> items = new ArrayList<>();
items.add("apple");
items.add("banana");
items.get(0);
items.size();
items.remove(0);
When to use
Ordered collections with indexed access and frequent append. For key lookup, use HashMap. For uniqueness, HashSet.
Important interview questions and answers
- Q: ArrayList vs LinkedList?
A: ArrayList faster random access; LinkedList better for frequent insert/delete in the middle (rare in practice—ArrayList wins most cases). - Q: Why generics?
A: Compile-time type safety—no casting from raw lists.
Self-check
- Which method adds an element?
- What package contains ArrayList?