Introduction to ArrayList in Java
ArrayList is one of the most used classes in the Java Collections Framework. It provides a dynamic array for storing elements, allowing both random access and flexible size management. Unlike regular arrays in Java, an ArrayList
can automatically resize itself as elements are added or removed, making it ideal for scenarios where the number of items can change over time.
Features of Java ArrayList
- Dynamic Resizing: No need to specify a fixed size up front.
- Indexed Access: Retrieve elements in constant time using an index.
- Allows Duplicates: Elements can be repeated.
- Maintains Insertion Order: Elements keep their original order.
- Works with Generics: Type-safe collections prevent runtime errors.
- Implements List Interface: Can be used anywhere a
List
is expected.
Syntax: How to Create an ArrayList
import java.util.ArrayList;
ArrayList<Type> listName = new ArrayList<>();
Example for storing integers:
ArrayList<Integer> numbers = new ArrayList<>();
Example for storing custom objects:
ArrayList<Student> students = new ArrayList<>();
Common ArrayList Operations
Operation | Description | Example Code |
---|---|---|
Add | Insert element at the end | list.add(value); |
Insert at index | Insert at specific position | list.add(index, value); |
Get element | Retrieve value by index | list.get(index); |
Set element | Update value at given index | list.set(index, value); |
Remove by index | Delete an element at position | list.remove(index); |
Remove by value | Delete the first matching object | list.remove(value); |
Size | Get number of elements | list.size(); |
Clear | Remove all elements | list.clear(); |
Contains | Check if value is present | list.contains(value); |
Example: Basic Usage of ArrayList
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); System.out.println(fruits); // Output: [Apple, Banana, Orange] fruits.remove("Banana"); System.out.println(fruits.get(1)); // Output: Orange System.out.println(fruits.size()); // Output: 2 } }
Advantages of ArrayList
- Flexible size compared to arrays.
- Easy element addition/removal (except for the middle elements, where removal can be slower).
- Built-in methods for searching, sorting, and updating elements.
Limitations & When to Avoid
- Not thread-safe: For concurrent use, use
Collections.synchronizedList()
orCopyOnWriteArrayList
. - Slower insertions/deletions in the middle of the list (shifting of elements required).
- Not for primitive types: Use wrappers (
Integer
,Double
, etc.) instead of primitives (int
,double
).
If you need frequent insertions/removals at both ends, consider using LinkedList
. For frequent random access and dynamic resizing, ArrayList
works best.
Java ArrayList vs. Array and LinkedList
Feature | Array | ArrayList | LinkedList |
---|---|---|---|
Size | Fixed | Dynamic | Dynamic |
Insertion/Removal | Manual, limited | Easy, slow in middle | Easy, fast in middle |
Access by index | Fast (O(1)) | Fast (O(1)) | Slow (O(n)) |
Memory | Compact | Slight overhead | Extra memory for links |
Conclusion
ArrayList is a versatile, flexible, and strongly supported part of Java’s Collections Framework. It’s most appropriate when you want to store elements in a list with frequent random access and only occasional insertions or removals.