The subList() method of java.util.ArrayList class is used to return 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.
Syntax:
public List 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 list.
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.
Example 1:
// Java program to demonstrate // subList() method // for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // Populating arrlist1 arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); arrlist.add("E"); // print arrlist System.out.println("Orignal arrlist: " + arrlist); // getting the subList // using subList() method List<String> arrlist2 = arrlist.subList(2, 4); // print the subList System.out.println("Sublist of arrlist: " + arrlist2); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown : " + e); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
Orignal arrlist: [A, B, C, D, E] Sublist of arrlist: [C, D]
Example 2: For IndexOutOfBoundsException
// Java program to demonstrate // subList() method // for IndexOutOfBoundsException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // Populating arrlist1 arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); arrlist.add("E"); // print arrlist System.out.println("Orignal arrlist: " + arrlist); // getting the subList // using subList() method System.out.println("\nEnd index value is out of range"); List<String> arrlist2 = arrlist.subList(2, 7); // print the subList System.out.println("Sublist of arrlist: " + arrlist2); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown : " + e); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
Orignal arrlist: [A, B, C, D, E] End index value is out of range Exception thrown : java.lang.IndexOutOfBoundsException: toIndex = 7
Example 3: For IllegalArgumentException
// Java program to demonstrate // subList() method // for IllegalArgumentException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // Populating arrlist1 arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); arrlist.add("E"); // print arrlist System.out.println("Orignal arrlist: " + arrlist); // getting the subList // using subList() method System.out.println("\nEndpoint indices " + "are out of order" + " (fromIndex > toIndex)"); List<String> arrlist2 = arrlist.subList(7, 2); // print the subList System.out.println("Sublist of arrlist: " + arrlist2); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown: " + e); } catch (IllegalArgumentException e) { System.out.println("Exception thrown: " + e); } } } |
Orignal arrlist: [A, B, C, D, E] Endpoint indices are out of order (fromIndex > toIndex) Exception thrown: java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)
Recommended Posts:
- ArrayList set() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList listIterator() method in Java with Examples
- ArrayList size() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList ensureCapacity() method in Java with Examples
- Vector subList() Method in Java
- Arraylist removeRange() in Java with examples
- ArrayList clear() in Java with examples
- ArrayList and LinkedList remove() methods in Java with Examples
- ArrayList spliterator() method in Java
- ArrayList retainAll() method in Java
- ArrayList forEach() method in Java
- ArrayList removeIf() method in Java
- Java.util.ArrayList.addall() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



