Java ArrayList
The ArrayList class is one of the most commonly used data structures in
Java. It belongs to the Java Collections Framework and is part of the
java.util package. An ArrayList implements the List interface, allowing
developers to store ordered collections of elements.
You can think of an ArrayList as a dynamic or resizable array. Unlike
traditional arrays in Java, an ArrayList can automatically expand or
shrink in size as elements are added or removed.
This flexibility makes ArrayList an excellent choice when the number of
elements in a collection is unknown or changes frequently.
Array vs ArrayList in Java
Before using ArrayList, it is useful to understand how it differs from
standard arrays.
Table
Feature
Size
Array
Fixed size
ArrayList
Dynamic size
Package
Flexibility
Built-in Methods
Core Java
Limited
Very few
java.util package
Highly flexible
Many useful methods
In short, ArrayList provides more functionality and flexibility than
traditional arrays.
Creating an ArrayList
To use an ArrayList in Java, you must first import it from the
java.util package.
Example: Creating an ArrayList
import java.util.ArrayList;
public class LibraryExample {
public static void main(String[] args) {
// Creating an ArrayList to store book
titles
ArrayList<String> books = new
ArrayList<>();
books.add("Java Fundamentals");
books.add("Data Structures
Guide");
books.add("Object-Oriented
Programming");
System.out.println("Book Collection: " +
books);
}
}
This program creates an ArrayList and stores a collection of book
titles.
Adding Elements to an ArrayList
Elements can be inserted into an ArrayList using the add()
method.
Example: Adding Elements
import java.util.ArrayList;
public class CityListDemo {
public static void main(String[] args) {
ArrayList<String> cities = new
ArrayList<>();
cities.add("Chennai");
cities.add("Mumbai");
cities.add("Delhi");
// Insert element at a specific
index
cities.add(1, "Bangalore");
System.out.println("Cities: " +
cities);
}
}
Key Point
ArrayList maintains insertion order, meaning elements appear in the
same order they were added.
Accessing Elements
To retrieve an element from an ArrayList, use the get() method and
specify the index.
Example
import java.util.ArrayList;
public class AccessElementDemo {
public static void main(String[] args) {
ArrayList<String> languages = new
ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
String firstLanguage =
languages.get(0);
System.out.println("First Language: " +
firstLanguage);
}
}
Indexes in Java start from 0, so the first element is always at index
0.
Updating Elements
You can modify an existing element using the set() method.
Example
import java.util.ArrayList;
public class UpdateElementDemo {
public static void main(String[] args) {
ArrayList<String> fruits = new
ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
// Replace Banana with Orange
fruits.set(1, "Orange");
System.out.println("Updated List: " +
fruits);
}
}
Removing Elements
Elements can be removed using the remove() method.
Example
import java.util.ArrayList;
public class RemoveElementDemo {
public static void main(String[] args) {
ArrayList<String> tasks = new
ArrayList<>();
tasks.add("Email client");
tasks.add("Prepare report");
tasks.add("Team meeting");
tasks.remove(1);
System.out.println("Remaining Tasks: " +
tasks);
}
}
To remove all elements, use:
tasks.clear();
Finding the Size of an ArrayList
To determine how many elements exist in the list, use the size()
method.
Example
import java.util.ArrayList;
public class SizeExample {
public static void main(String[] args) {
ArrayList<String> courses = new
ArrayList<>();
courses.add("AI");
courses.add("Machine
Learning");
courses.add("Data Science");
System.out.println("Total Courses: " +
courses.size());
}
}
Looping Through an ArrayList
You can iterate through ArrayList elements using different looping
techniques.
Using a Standard For Loop
import java.util.ArrayList;
public class LoopExample {
public static void main(String[] args) {
ArrayList<String> devices = new
ArrayList<>();
devices.add("Laptop");
devices.add("Tablet");
devices.add("Smartphone");
for(int i = 0; i < devices.size(); i++)
{
System.out.println(devices.get(i));
}
}
}
Using a For-Each Loop
import java.util.ArrayList;
public class ForEachExample {
public static void main(String[] args) {
ArrayList<String> tools = new
ArrayList<>();
tools.add("Hammer");
tools.add("Screwdriver");
tools.add("Wrench");
for(String tool : tools) {
System.out.println(tool);
}
}
}
The for-each loop is often preferred because it is cleaner and easier
to read.
Using ArrayList with Different Data Types
ArrayList stores objects, not primitive types. Therefore, primitive
values must use wrapper classes.
Table
Primitive Type Wrapper Class
int Integer
double Double
char Character
boolean Boolean
Example: ArrayList with Numbers
import java.util.ArrayList;
public class ScoreExample {
public static void main(String[] args) {
ArrayList<Integer> scores = new
ArrayList<>();
scores.add(85);
scores.add(90);
scores.add(78);
scores.add(92);
for(Integer score : scores) {
System.out.println(score);
}
}
}
Sorting an ArrayList
The Collections class provides utility methods for working with
collections, including sorting.
Example: Sorting Strings
import java.util.ArrayList;
import java.util.Collections;
public class SortNamesExample {
public static void main(String[] args) {
ArrayList<String> names = new
ArrayList<>();
names.add("Zara");
names.add("Aman");
names.add("Kiran");
names.add("Dev");
Collections.sort(names);
System.out.println("Sorted
Names:");
for(String name : names) {
System.out.println(name);
}
}
}
Example: Sorting Numbers
import java.util.ArrayList;
import java.util.Collections;
public class SortNumbersExample {
public static void main(String[] args) {
ArrayList<Integer> values = new
ArrayList<>();
values.add(45);
values.add(12);
values.add(67);
values.add(23);
Collections.sort(values);
System.out.println(values);
}
}
The var Keyword (Java 10+)
Starting from Java 10, the var keyword allows developers to declare
variables without repeating the type.
Example
var languages = new ArrayList<String>();
languages.add("Java");
languages.add("Python");
The compiler automatically determines the variable type.
However, many developers still prefer writing the explicit type for
readability and maintainability.
Using List with ArrayList
In many Java programs, developers declare variables using the List
interface rather than the ArrayList class.
Example
import java.util.List;
import java.util.ArrayList;
public class InterfaceExample {
public static void main(String[] args) {
List<String> subjects = new
ArrayList<>();
subjects.add("Mathematics");
subjects.add("Physics");
subjects.add("Computer
Science");
System.out.println(subjects);
}
}