AbstractSequentialList subList() method in Java with Example
The subList() method of AbstractSequentialList in Java is used to get a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
protected List<E> subList(int fromIndex, int toIndex)
Parameters: These method takes two parameters:
- fromIndex: starting index from which elements are to be fetched.
- toIndex: ending index from which elements are to be fetched.(exclusive)
Return Value: This method returns a view of the specified range within this list
Exception: This method throws:
- IndexOutOfBoundsException: if an endpoint index value is out of range.
- IllegalArgumentException: if the endpoint indices are out of order.
Below examples illustrates AbstractSequentialList.subList() method:
AbstractSequentialList: [1, 2, 3, 12, 9, 13] subList of 1st 2 elements: [1, 2]
AbstractSequentialList: [1, 2, 3, 12, 9, 13] Trying to get subList of 11th elements: java.lang.IndexOutOfBoundsException: toIndex = 11
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.