Sitemap

ArrayList in Java

2 min readJun 4, 2025

--

Press enter or click to view image in full size
Photo by Andrew Neel on Unsplash

What is an ArrayList?

  • ArrayList is a class in Java from the java.util package.
  • It is a resizable array, which means it can grow or shrink in size automatically.
  • Unlike regular arrays, you don’t need to specify the size when you create an ArrayList.

Key Features

  • Dynamic Size: Grows as you add more elements.
  • Indexed Access: You can access elements by their index (like an array).
  • Allows Duplicates: You can store duplicate elements.
  • Order Maintained: Elements are stored in the order you add them.
  • Type-Safe: You can specify the type of elements it holds using generics (e.g., ArrayList<String>).

Basic Operations

  1. Importing
import java.util.ArrayList;

2. Creating an ArrayList

ArrayList<String> list = new ArrayList<>();

3. Adding Elements

list.add("Apple");
list.add("Banana");

4. Accessing Elements

String fruit = list.get(0); // "Apple"

5. Changing Elements

list.set(1, "Cherry"); // Changes "Banana" to "Cherry"

6. Removing Elements

list.remove(0); // Removes "Apple"

7. Size of ArrayList

int size = list.size();

Get Saumya14’s stories in your inbox

Join Medium for free to get updates from this writer.

8. Looping through ArrayList

for (String item : list) {
System.out.println(item);
}

Example

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
// Create an ArrayList of Integers
ArrayList<Integer> arrList = new ArrayList<>();

// Add elements to the ArrayList
arrList.add(10);
arrList.add(20);
arrList.add(30);

// Print all elements
System.out.println("All numbers: " + arrList);

// Print individual elements
System.out.println("First element: " + arrList.get(0));
System.out.println("Second element: " + arrList.get(1));
System.out.println("Third element: " + arrList.get(2));

// Loop through the ArrayList
System.out.println("Looping through elements:");
for (int num : arrList) {
System.out.println(num);
}
}
}

Key Concepts

1. ArrayList vs. Array

  • ArrayList is dynamic (can grow/shrink), while arrays have a fixed size.
  • ArrayList can only store objects (not primitives), but Java autoboxes primitives like int to Integer automatically.

2. Common Methods

  • add(element): Adds to the end.
  • add(index, element): Adds at a specific position.
  • remove(index): Removes element at index.
  • remove(Object o): Removes the first occurrence of the object.
  • contains(element): Checks if element exists.
  • indexOf(element): Returns the index of the first occurrence.
  • clear(): Removes all elements.
  • isEmpty(): Checks if the list is empty.

3. Performance

  • Accessing elements by index is fast (O(1)).
  • Adding/removing at the end is fast.
  • Adding/removing in the middle or beginning can be slow (O(n)), as elements may need to be shifted.

4. Null Values

  • ArrayList can store null values.

5. Conversion

  • Convert to array: Integer[] arr = numbers.toArray(new Integer[0]);
  • Convert from array: ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr));

6. Sorting

  • You can sort an ArrayList using Collections.sort(list);

--

--