Java ArrayList subList() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, subList(fromIndex, toIndex) returns a List holding the elements from index fromIndex (included) up to toIndex (excluded) of an ArrayList.

The result is a view, not a copy: it shares storage with the original list. Setting, sorting, or clearing through the view changes the original. To get an independent list, wrap it: new ArrayList<>(list.subList(a, b)).

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

The start index is included, the end index is not.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
List<String> mid = list.subList(1, 3);              // [b, c] (a view)
List<String> copy = new ArrayList<>(mid);           // independent copy
list.subList(0, 2).clear();                         // list is now [c, d]
CallWhat it does
list.subList(a, b)A view of indexes a to b - 1.
new ArrayList<>(list.subList(a, b))An independent copy of that range.
list.subList(a, b).clear()Removes a range of elements from the list.
list.subList(a, b).sort(c)Sorts only that part of the list.

The view has toIndex - fromIndex elements, and its own indexes start at 0. subList(i, i) is a valid empty view.

Library declarations
Java
// java.util.ArrayList<E>
public List<E> subList(int fromIndex, int toIndex)

// Declared in List<E>

Parameters

ParameterDescription
fromIndex (int)First index of the range (inclusive).
toIndex (int)End of the range (exclusive). Must not be greater than size().

Return Value

Return ValueDescription
List<E>A view of the range, backed by the original list.

Compare with Similar Methods

Working with part of a list:

MethodHow it differs
clear()Called on a subList, removes just that range.
clone()Copies the whole list, not a view.
toArray()Copies elements into an array.
removeIf(filter)Removes by condition instead of by index range.

Java Examples

Example 1 — A View Writes Through; a Copy Does Not

Take the weekend as a view and change it, then change a copy of the weekdays.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListSubListDemo {
    public static void main(String[] args) {
        List<String> week = new ArrayList<>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));

        List<String> weekend = week.subList(5, 7);
        System.out.println("weekend: " + weekend + " size=" + weekend.size());

        weekend.set(0, "SAT");
        System.out.println("week after set: " + week);

        List<String> weekdays = new ArrayList<>(week.subList(0, 5));
        weekdays.set(0, "MON");
        System.out.println("copy changed, week still starts with " + week.get(0));
    }
}

How It Works

  1. Half-open range. subList(5, 7) covers indexes 5 and 6, so it has 7 - 5 = 2 elements.
  2. Shared storage. weekend.set(0, "SAT") writes to index 0 of the view, which is index 5 of week.
  3. Real copy. Wrapping the view in new ArrayList<>(...) copies the five weekdays into a new list.
  4. Independent. Changing the copy's first day leaves week starting with Mon.

Example 2 — Sort, Clear, and Search Part of a List

Use short-lived views to work on index ranges.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListSubListRange {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(9, 4, 7, 1, 8, 2, 6));

        nums.subList(1, 4).sort(null);
        System.out.println("sort indexes 1-3: " + nums);

        nums.subList(4, 6).clear();
        System.out.println("clear indexes 4-5: " + nums);

        System.out.println("7 in first three? " + nums.subList(0, 3).contains(7));
    }
}

How It Works

  1. Partial sort. The view of indexes 1 to 3 holds 4, 7, 1; sorting it puts 1, 4, 7 back into those same slots.
  2. Remove a range. Clearing the view of indexes 4 and 5 deletes 8 and 2 from nums and shifts 6 left.
  3. Search a range. The first three elements are now 9, 1, 4, so 7 is not among them.
  4. One view per job. Each view is created, used once, and dropped, so a later change to nums never invalidates one that is still in use.

Example 3 — What Fails: Stale Views and Reversed Indexes

Change the list after creating a view, then pass indexes in the wrong order.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;

public class ArrayListSubListStale {
    public static void main(String[] args) {
        List<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
        List<String> firstTwo = letters.subList(0, 2);

        letters.add("e");
        try {
            firstTwo.size();
        } catch (ConcurrentModificationException e) {
            System.out.println("old view: " + e.getClass().getSimpleName());
        }

        System.out.println("fresh view: " + letters.subList(0, 2));

        try {
            letters.subList(3, 1);
        } catch (IllegalArgumentException e) {
            System.out.println("subList(3, 1): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. View first. firstTwo is created while letters has four elements.
  2. Direct change. letters.add("e") changes the size of the original list without going through the view.
  3. Stale view. The next use of firstTwo, even just size(), detects the change and throws ConcurrentModificationException. Creating a fresh view works fine.
  4. Reversed indexes. A start index greater than the end index is an IllegalArgumentException, not an index-range error.

Frequently Asked Questions

No. It returns a view backed by the original list. Wrap it in new ArrayList<>(...) for an independent copy.
No. fromIndex is included and toIndex is excluded, so subList(1, 3) holds indexes 1 and 2.
Call list.subList(from, to).clear().
The original list was structurally changed after the view was created. Create a new view after the change.
It throws IllegalArgumentException because the start index is greater than the end index.

Next: toArray()

Copy the elements into an array.

toArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful