Data Structures & Algorithms 1 / 32
What is the difference between Array and ArrayList in Java?
Key Differences:
1. Size:
Array: Fixed size, must be specified at creation
ArrayList: Dynamic size, grows automatically
2. Type:
Array: Can hold primitives (int, char) and objects
ArrayList: Can only hold objects (uses wrapper classes for primitives)
3. Performance:
Array: Faster for fixed-size collections
ArrayList: Slightly slower due to resizing overhead
4. Methods:
Array: No built-in methods, only length property
ArrayList: Rich API with add(), remove(), contains(), etc.
Example:
// Array
int[] arr = new int[5];
arr[0] = 10;
// ArrayList
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
When to use:
Use Array when size is known and fixed
Use ArrayList when size varies or you need built-in methods