Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Use subList to remove elements at indexes 10 to 19 from an ArrayList in one call, and explain why it is efficient.

The one call is list.subList(10, 20).clear(). subList returns a view of indexes 10 to 19 (from inclusive, to exclusive), and changes through the view go straight to the backing list. It is efficient because ArrayList implements clear on the view with its internal removeRange, which shifts the tail of the array once, instead of ten separate remove calls that would each shift the tail.

Why this question is asked

Removing a range of elements is common in real code: dropping a page of results, trimming a buffer, cutting a window out of a log. Many developers write a loop of remove calls without realising each one moves the rest of the array. Interviewers ask this to see whether we know list views and the cost model of ArrayList. In this answer, we will recap what subList returns, remove the range in one call, look at what happens inside ArrayList, compare it with the loop, and finish with the pitfalls of views.

1A Quick Recap of subList

list.subList(from, to) returns a List that shows part of another list, from index from inclusive up to index to exclusive. So subList(10, 20) covers indexes 10, 11 and so on up to 19, which is exactly ten elements, and its size is to minus from.

The important word is view. subList does not copy anything. The returned object holds a reference to the original list, called the backing list, plus an offset and a size. Reading the view reads the backing list, and changing the view changes the backing list.

Java
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 30; i++) list.add(i); // [0, 1, 2, ..., 29]
List<Integer> view = list.subList(10, 20);
System.out.println(view); // [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
System.out.println(view.size()); // 10
view.set(0, 99);
System.out.println(list.get(10)); // 99: the change went to the backing list

2Removing the Range in One Call

Because the view writes through, clearing it removes those elements from the backing list.

Java
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 30; i++) list.add(i);
list.subList(10, 20).clear();
System.out.println(list.size()); // 20
System.out.println(list); // [0, 1, ..., 9, 20, 21, ..., 29]
System.out.println(list.get(10)); // 20: the old element 20 moved down to index 10

One line, no index arithmetic, and no chance of the off-by-one errors that loops over shrinking lists invite. The same pattern works for any range operation: list.subList(0, 5).clear() drops the first five, list.subList(list.size() - 3, list.size()).clear() drops the last three.

3Why It Is Efficient: removeRange

Inside, clear() on a view calls removeRange(from, to) on the backing list. ArrayList implements removeRange with one System.arraycopy: it moves every element after index 19 down by ten places in one block, then sets the ten now-unused slots at the end to null so the removed objects can be garbage collected, and lowers size by ten. It also increases modCount, the counter of structural changes, once.

removeRange itself is a protected method of AbstractList, so we cannot call it directly on an ArrayList. subList().clear() is the public way to reach it, and that is the reason the idiom exists.

Step throughWhat list.subList(10, 20).clear() does
CallerSubList viewArrayList
subList(10, 20) creates the view
clear()
removeRange(10, 20)
arraycopy: move 10 tail elements down 10 slots
null out 10 slots, size -= 10, modCount++
list is [0..9, 20..29]

6 messages in order. Play them, or step through them one at a time.

One block move, whatever the size of the range.

4Comparing It With a Loop of remove Calls

The loop people often write instead is shown below. It works, but every remove(10) shifts all the elements after index 10 down by one place.

Java
// Works, but does ten separate shifts
for (int i = 0; i < 10; i++) {
list.remove(10); // each call moves the whole tail one slot left
}
// Broken version that many people write first
for (int i = 10; i < 20; i++) {
list.remove(i); // removes 10, 12, 14, ... because the list shifts under i
}
Removing k elements starting at index p
ApproachElement movesNotes
subList(p, p + k).clear()about n - p, onceone arraycopy
k calls to remove(p)about k x (n - p)correct but slow for large k
remove(i) for i in p..p+kwrong elements removedindexes shift under the loop

For a list of n elements, removing k elements one at a time from position p costs about k times (n minus p) element moves, while removeRange costs about n minus p, once. With a million-element list and k = 1,000 removed near the front, that is about a billion moves versus about a million. The second loop shows another danger: after each remove, the elements shift down, so incrementing i skips every other element and ends up removing the wrong ones.

For LinkedList, subList(10, 20).clear() is also the clean way. It walks to index 10 once and unlinks ten nodes in sequence, instead of walking from the start for each remove(10).

5Pitfalls of Using Views

Views are powerful because they write through, and that is also where the traps are. If we keep a subList and then structurally change the backing list directly, for example by adding or removing an element, the view becomes invalid and its next use throws ConcurrentModificationException. So we create the view and use it immediately, as in subList(10, 20).clear(), rather than storing it.

Also check the bounds: subList throws IndexOutOfBoundsException if from is negative, to is larger than size, and IllegalArgumentException if from is greater than to. And on an immutable list such as List.of, clear() on the view throws UnsupportedOperationException, just as it would on the list itself.

6Follow-up questions

Does subList(10, 20).clear() work on every List?

It works on any modifiable list that supports removal. On ArrayList it uses the fast removeRange, and on LinkedList it unlinks nodes after one walk. On Arrays.asList and List.of it throws UnsupportedOperationException, because those lists cannot shrink.

How would we remove elements by a condition instead of by index?

We would use list.removeIf(predicate). ArrayList implements it in a single pass that compacts the survivors once, so it is also O(n) however many elements match. subList().clear() is for contiguous index ranges, and removeIf for conditions.

Can we sort or modify just part of a list the same way?

Yes. Any operation on the view applies to that range of the backing list. For example, Collections.sort(list.subList(0, 5)) or list.subList(0, 5).sort(null) sorts only the first five elements, and Collections.reverse(list.subList(3, 8)) reverses a middle section.

7Conclusion

In this answer, we saw that subList(from, to) is a write-through view of indexes from inclusive to to exclusive, so list.subList(10, 20).clear() removes indexes 10 to 19 from the backing list in one call. ArrayList implements it with removeRange, a single block move of the tail, instead of the ten shifts a loop of remove calls makes, and it avoids the classic skipping bug of removing by a rising index. Views should be used immediately, not stored.

What to say in the interviewAbout 90 seconds aloud

Open with

I would write list.subList(10, 20).clear(). subList returns a view of the backing list from index 10 inclusive to 20 exclusive, so it covers exactly indexes 10 through 19, ten elements.

Then, if they want more

It is a view, not a copy, so any change through it, including clear, is applied to the original list. Afterwards the list has ten fewer elements and what used to be at index 20 is now at index 10. The reason it is efficient is that clear on a view calls removeRange on the backing list.

ArrayList implements removeRange with one System.arraycopy that moves the whole tail down by ten places at once, then nulls out the freed slots so the objects can be garbage collected, and updates size and modCount once. removeRange is protected, so this idiom is the public way to reach it.

The alternative, calling remove ten times, shifts the tail ten times, so for k removals it costs k times the tail length instead of the tail length once. And the naive loop that removes at a rising index is actually wrong, because the elements shift under it and it removes every other one.

I would use the view immediately rather than keeping it, since a structural change to the backing list invalidates it, and I would remember it throws on immutable lists like List.of.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Use subList to remove elements at indexes 10 to 19 from an ArrayList in one call, and...": https://scrivio-interview-library.netlify.app/java/lists/8b6bcae810/