In this tutorial, we will see how to get a sublist from an existing ArrayList. We will be using the subList()
method of ArrayList class.
Syntax:
List subList(int fromIndex, int toIndex)
Here fromIndex
is inclusive and toIndex
is exclusive. There are few important points regarding this method which I have shared at the end of this post.
Note:
The subList method throws IndexOutOfBoundsException
– if the specified indexes are out of the range of ArrayList (fromIndex < 0 || toIndex > size)
.
IllegalArgumentException
– if the starting index is greater than the end point index (fromIndex > toIndex)
.
Example 1: Getting a sublist from an ArrayList
In the following example, we have an ArrayList al
. We want to get a sublist starting from index 1 till index 3 from this ArrayList. To get this sublist, we are calling the subList()
method like this: al.subList(1, 4)
.
As mentioned in the syntax of sublist(), the fromIndex is inclusive and toIndex is exclusive, which means index 1 is inclusive and index 4 is exclusive. This is why element present at index 1 is included in sublist, while element present at index 4 is excluded from ArrayList and elements are present till index 3.
We placed the code inside a try-catch block to catch any exception. In this program, indexes mentioned in the subList()
method are valid so no exception raised. In the example 2 and 3, we will see how to handle exception when index values are invalid.
import java.util.ArrayList; import java.util.List; public class JavaExample { public static void main(String a[]){ try { ArrayList<String> al = new ArrayList<String>(); //Addition of elements in ArrayList al.add("Steve"); al.add("Justin"); al.add("Ajeet"); al.add("John"); al.add("Arnold"); al.add("Chaitanya"); System.out.println("Original ArrayList Content: " + al); //Storing Sublist into another ArrayList ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4)); System.out.println("SubList stored in ArrayList: " + al2); //Storing Sublist into a List List<String> list = al.subList(1, 4); System.out.println("SubList stored in List: " + list); } catch (IndexOutOfBoundsException e) { System.out.println("Exception while getting sublist: " + e); } catch (IllegalArgumentException e) { System.out.println("Exception while getting sublist: " + e); } } }
Output:
Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya] SubList stored in ArrayList: [Justin, Ajeet, John] SubList stored in List: [Justin, Ajeet, John]
Example 2: subList() method: IndexOutOfBoundsException
As mentioned in the beginning, if the specified indexes in subList()
method are out of the range, this method throws IndexOutOfBoundsException
. Exception occurs if index values satisfies this condition: (fromIndex < 0 || toIndex > size)
import java.util.ArrayList; import java.util.List; public class JavaExample { public static void main(String a[]){ try { ArrayList<String> al = new ArrayList<String>(); //Addition of elements in ArrayList al.add("Steve"); al.add("Justin"); al.add("Ajeet"); al.add("John"); al.add("Arnold"); al.add("Chaitanya"); System.out.println("Original ArrayList Content: " + al); // The toIndex is 14 and size of the ArrayList is 6 so // toIndex > size, this will throw exception List<String> list = al.subList(1, 14); System.out.println("SubList: " + list); } catch (IndexOutOfBoundsException e) { System.out.println("Exception while getting sublist: " + e); } catch (IllegalArgumentException e) { System.out.println("Exception while getting sublist: " + e); } } }
Output:
Example 3: subList() method: IllegalArgumentException
If the starting index is greater than the end index (fromIndex > toIndex)
then IllegalArgumentException
occurs. Let’s see the following example to understand when this exception occurs.
import java.util.ArrayList; import java.util.List; public class JavaExample { public static void main(String a[]){ try { ArrayList<String> names = new ArrayList<String>(); //Addition of elements in ArrayList names.add("Paul"); names.add("Rahul"); names.add("Kevin"); names.add("Smith"); names.add("Rob"); System.out.println("Original ArrayList Content: " + names); // Here fromIndex is 4 and toIndex is 2 // fromIndex > toIndex, this will throw exception List<String> list = names.subList(4, 2); System.out.println("SubList: " + list); } catch (IndexOutOfBoundsException e) { System.out.println("Exception while getting sublist: " + e); } catch (IllegalArgumentException e) { System.out.println("Exception while getting sublist: " + e); } } }
Output:
Abhishek Basu says
//Sublist to ArrayList
ArrayList al2 = new ArrayList(al.subList(1, 4));
How is the above method working? I know that the below would not work because of downcasting issues.
ArrayList al2 = (ArrayList) (al.subList(1, 4));
Paal says
Lets say I have a list of unknown length I want to get the whole list except the last element. Will it work to do this:
ArrayList al2 = new ArrayList(al.subList(1, -2));