The subList() method of Java.util.Stack class is used to return a view of the portion of this Stack between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned Stack is empty.)
The returned Stack is backed by this Stack, so non-structural changes in the returned Stack are reflected in this Stack, and vice-versa. The returned Stack supports all of the optional Stack operations.
public Stack subList(int fromIndex, int toIndex)
Parameters: This method takes the following argument as a parameter.
- fromIndex – low endpoint (inclusive) of the subList
- toIndex – high endpoint (exclusive) of the subList
Returns Value: This method returns a view of the specified range within this Stack.
Exception: This method throws the following Exception.
- IndexOutOfBoundsException – if an endpoint index value is out of range (fromIndex size)
- IllegalArgumentException – if the endpoint indices are out of order (fromIndex > toIndex)
Below are the examples to illustrate the subList() method.
Original stack: [A, B, C, D, E] SubStack of stack: [C, D]
Example 2: For IndexOutOfBoundsException
Original stack: [A, B, C, D, E] End index value is out of range java.lang.IndexOutOfBoundsException: toIndex = 7
Example 3: For IllegalArgumentException
Original stack: [A, B, C, D, E] Endpoint indices are out of order (fromIndex > toIndex) java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)
- AbstractSequentialList subList() method in Java with Example
- Vector subList() Method in Java
- ArrayList subList() method in Java with Examples
- AbstractList subList() method in Java with Examples
- CopyOnWriteArrayList subList() method in Java with Examples
- List sublist() Method in Java with Examples
- How to remove a SubList from a List in Java
- Stack set() method in Java with Example
- Stack get() method in Java with Example
- Stack contains() method in Java with Example
- Stack pop() Method in Java
- Stack addElement(E) method in Java with Example
- Stack elements() method in Java with Example
- Stack clear() method in Java with Example
- Stack containsAll() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.