In Java, subList(fromIndex, toIndex) returns a List that shows the part of the LinkedList from fromIndex (included) up to toIndex (not included).
The result is a view, not a copy. Reading it reads the original, and changes made through the view, such as set or clear(), change the original list. To get an independent list, copy it with new LinkedList<>(list.subList(a, b)).
An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.
Syntax
Give the start index and the end index (not included).
Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C", "D", "E"));
List<String> part = list.subList(1, 4); // [B, C, D]
part.clear(); // the original is now [A, E]
List<String> copy = new LinkedList<>(list.subList(0, 1)); // independent copy
Call
What it does
list.subList(from, to)
A view of the elements from from up to, but not including, to.
list.subList(from, to).clear()
Removes that range from the original list.
new LinkedList<>(list.subList(from, to))
Makes an independent copy of the range.
list.subList(0, n)
The first n elements.
The size of the view is toIndex - fromIndex. An empty range such as subList(2, 2) is allowed.
Library declarations
Java
// java.util.LinkedList (inherited from AbstractList)
public List<E> subList(int fromIndex, int toIndex)
Parameters
Parameter
Description
fromIndex (int)
The first index of the range (included).
toIndex (int)
The end of the range (not included). Can be as large as size().
Return Value
Return Value
Description
List<E>
A view of the range. It is backed by the original list.