Difference between Array and ArrayList in Java
π‘ Concept: Array vs ArrayList
In Java, Array
is a fixed-size data structure, while ArrayList
is a resizable collection class that is part of the Java Collections Framework.
π Quick Intro
Arrays are fast and memory-efficient but cannot change size after creation. ArrayLists are flexible, allow dynamic resizing, and offer utility methods but have slightly more overhead.
π§ Analogy
Think of an Array like a fixed-length trainβyou can't add more compartments. An ArrayList is like a flexible bus that can grow and shrink as needed during the ride.
π§ Technical Explanation
- Array is a basic structure with a fixed size declared at creation.
- ArrayList is a part of the Java Collections Framework and allows dynamic resizing.
- Array supports both primitives and objects; ArrayList supports only objects.
- ArrayList provides built-in methods like
add()
,remove()
,contains()
, etc. - Array is faster for direct access; ArrayList is more flexible.
π― Use Cases
- β Use Array when size is fixed and performance is critical
- β Use ArrayList when you need dynamic resizing or collection utilities
- β Use ArrayList in collections-based APIs and loops
π» Example: Array vs ArrayList
// Using Array
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
// Using ArrayList
import java.util.ArrayList;
ArrayList<String> nameList = new ArrayList<>();
nameList.add("Alice");
nameList.add("Bob");
nameList.add("Charlie");

β Interview Q&A
Q1: What is the main difference between Array and ArrayList?
A: Array is fixed-size; ArrayList is resizable.
Q2: Can Array hold primitives?
A: Yes.
Q3: Can ArrayList hold primitives directly?
A: No, it holds objects (autoboxing used).
Q4: Which has better performance for direct access?
A: Array.
Q5: Is Array part of Collections Framework?
A: No.
Q6: Is ArrayList type-safe?
A: Yes, with generics.
Q7: Can we resize an array?
A: No.
Q8: Which provides methods like add() and remove()?
A: ArrayList.
Q9: Is ArrayList slower than Array?
A: Slightly, due to dynamic features.
Q10: Can ArrayList be used in for-each loop?
A: Yes.
π MCQs
Q1. Which is resizable: Array or ArrayList?
- Array
- ArrayList
- Both
- None
Q2. Can Array hold primitive types?
- No
- Yes
- Only integers
- Only strings
Q3. Is ArrayList part of Collections Framework?
- No
- Yes
- Partially
- Only in JDK 11+
Q4. Which supports add() method?
- Array
- ArrayList
- Both
- None
Q5. Which is better for direct indexing?
- Array
- ArrayList
- Set
- Map
Q6. Can ArrayList hold primitive types directly?
- Yes
- No
- Only int
- Only double
Q7. Which one is fixed in size?
- ArrayList
- Array
- HashMap
- Set
Q8. Does ArrayList allow duplicates?
- No
- Yes
- Only nulls
- Only strings
Q9. Which supports generics?
- Array
- ArrayList
- Both
- None
Q10. Which is more memory efficient?
- Array
- ArrayList
- Depends
- Same
π‘ Bonus Insight
Use Arrays.asList()
to quickly convert an array to a List, but remember it returns a fixed-size list β not a full ArrayList.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!