π 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.util
package, 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
ArrayList
is 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 :