
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java ArrayList subList() Method
Description
The Java ArrayList subList() method returns a view of the portion of this list using the fromIndex(inclusive), and toIndex(exclusive). If fromIndex and toIndex are equal, it will return empty list. Any non-structural changes in the returned list are reflected in the original list as well, and vice-versa. The returned list supports list operations as well.
Declaration
Following is the declaration for java.util.ArrayList.subList() method
public List<E> subList(int fromIndex, int toIndex)
Parameters
fromIndex − The index of the start element (inclusive).
toIndex − The index of the end element (exclusive) .
Return Value
This method returns a view of the specified range within this list.
Exception
IndexOutOfBoundsException − if an endpoint index value is out of range (fromIndex < 0 || toIndex > size).
IllegalArgumentException − if the endpoint indices are out of order (fromIndex > toIndex).
Example 1
The following example shows the usage of Java ArrayList subList() method. We're creating a ArrayList of Integers. We're adding couple of Integers to the ArrayList object using add() method calls per element and using subList() method, we're getting a portion of the list and printing that portion in the end.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<Integer> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(0); arrayList.add(1); arrayList.add(2); arrayList.add(3); arrayList.add(4); arrayList.add(5); arrayList.add(6); // get and print a sublist System.out.println(arrayList.subList(1,3)); } }
Output
Let us compile and run the above program, this will produce the following result −
[1, 2]
Example 2
The following example shows the usage of Java ArrayList subList() method. We're creating a ArrayList of Strings. We're adding couple of Strings to the ArrayList object using add() method calls per element and using subList() method, we're getting a portion of the list and printing that portion in the end.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<String> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); arrayList.add("F"); // get and print a sublist System.out.println(arrayList.subList(1,3)); } }
Output
Let us compile and run the above program, this will produce the following result −
[B, C]
Example 3
The following example shows the usage of Java ArrayList subList() method. We're creating a ArrayList of Student objects. We're adding couple of Student objects to the ArrayList object using add() method calls per element and using subList() method, we're getting a portion of the list and printing that portion in the end.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty arrayList ArrayList<Student> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(new Student(1, "Julie")); arrayList.add(new Student(2, "Robert")); arrayList.add(new Student(3, "Adam")); // get and print a sublist System.out.println(arrayList.subList(1,3)); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result −
[[ 2, Robert ], [ 3, Adam ]]