Java ArrayList β Complete Guide with Syntax, Examples & Best Practices
Introduction β Why Java ArrayList Is a Must-Know Collection
When building Java apps, you often need to store, access, and manipulate dynamic collections of data. Thatβs where ArrayList comes in β a powerful, resizable array from Javaβs Collection Framework.
The ArrayList class is widely used because of its simplicity, flexibility, and random-access performance.
By the end of this article, you’ll understand:
What Java ArrayList is and how it works
How to declare, initialize, and manipulate ArrayLists
Key methods like add(), remove(), get(), contains()
Best practices, performance tips, and real-world use cases
What Is ArrayList in Java?
An ArrayList in Java is a resizable array, part of
java.utilpackage, which allows duplicate elements and preserves insertion order.
It implements the List interface and supports generics for type safety.
Basic Syntax β Creating an ArrayList
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>(); // Using Generics
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits); // Output: [Apple, Banana]
}
}
Explanation:
ArrayList<String>: Declares a list of Stringsadd(): Adds elements to the list- Insertion order is preserved
Common ArrayList Methods
| Method | Description | Example |
|---|---|---|
add(E e) | Adds element at the end | list.add("Mango") |
add(int index, E e) | Inserts at specific position | list.add(1, "Kiwi") |
get(int index) | Returns element at index | list.get(0) |
set(int index, E e) | Replaces element at index | list.set(1, "Orange") |
remove(int index) | Removes element at index | list.remove(0) |
contains(Object o) | Checks if element exists | list.contains("Apple") |
indexOf(Object o) | Returns index of element | list.indexOf("Apple") |
clear() | Removes all elements | list.clear() |
size() | Returns number of elements | list.size() |
isEmpty() | Checks if list is empty | list.isEmpty() |
Looping Through an ArrayList
Using for loop
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
Using for-each loop
for (String fruit : fruits) {
System.out.println(fruit);
}
Using forEach() method (Java 8+)
fruits.forEach(System.out::println);
ArrayList vs Array
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic/resizable |
| Data types | Supports primitives | Supports objects only |
| Length | array.length | list.size() |
| Performance | Slightly faster | More flexible |
| Memory | Less overhead | Uses extra memory internally |
Real-World Example β Student List
import java.util.ArrayList;
public class StudentApp {
public static void main(String[] args) {
ArrayList<String> students = new ArrayList<>();
students.add("Aman");
students.add("Sneha");
students.add("Ravi");
if (students.contains("Sneha")) {
System.out.println("Sneha is enrolled.");
}
}
}
Useful in apps like:
- E-commerce carts
- To-do lists
- Dynamic forms
- Real-time search suggestions
Performance Tips
Use ArrayList when:
- You need fast access (
get()is O(1)) - You rarely insert/remove in the middle (those are O(n))
If frequent inserts/removals at the beginning/middle are needed, consider using LinkedList.
Summary
ArrayListis a dynamic array structure in Java that supports ordered and duplicate elements- Ideal for fast random access and flexible list operations
- Comes with powerful built-in methods for management and iteration
- Widely used in real-world Java applications for storing object collections
FAQs β Java ArrayList
Can ArrayList hold primitive types?
No. Use wrapper classes like Integer, Double, etc. (autoboxing handles this automatically).
Is ArrayList synchronized?
No. Use Collections.synchronizedList() or CopyOnWriteArrayList for thread safety.
Can ArrayList store null?
Yes, ArrayList allows null values.
What is the default size of an ArrayList?
Internally, the capacity starts at 10 (but size is 0 until elements are added).
How to convert ArrayList to array?
String[] arr = fruits.toArray(new String[0]);
Share Now :
