Java ArrayList sublist example shows how to get sublist of ArrayList in Java. The example also shows how to convert List object returned by sublist to ArrayList object.
How to get a sublist of ArrayList in Java?
Use the ArrayList subList
method to get the sublist from ArrayList.
1 |
public List<E> subList(int startIndex, int endIndex) |
This method returns sublist containing elements between startIndex and endIndex. Please note that the startIndex is inclusive while the endIndex is exclusive.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.List; public class ArrayListSubListExample { public static void main(String[] args){ //create new ArrayList ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); //add data to ArrayList aListNumbers.add(0); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(4); aListNumbers.add(5); aListNumbers.add(6); aListNumbers.add(7); aListNumbers.add(8); aListNumbers.add(9); /* * To get sublist of ArrayList, use * public List<E> subList(int start, int end) method. */ List<Integer> sublist = aListNumbers.subList(0, 5); /* * Important: Start index is inclusive but end index is exclusive. * So in our example, element at 0 index will be included but element * at 5th index will not be included in the sublist. */ System.out.println(sublist); } } |
Output
1 |
[0, 1, 2, 3, 4] |
Note:
ArrayList subList
method may throw IndexOutOfBoundsException if either of the index value specified is out of the range. That is if the specified start index is less than 0 or the end index is greater than the ArrayList size.
This method may also throw IllegalArgumentException if the specified end index is greater than the start index. For example, subList(7, 2).
How to convert the sublist List object to ArrayList?
As you may have observed, subList
method returns an object of the List instead of the ArrayList class. If you try to downcast the result of subList
method to ArrayList object, you may get a similar exception given below.
1 |
List<Integer> sublist = (ArrayList<Integer>) aListNumbers.subList(0, 5); |
1 2 |
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList$SubList cannot be cast to java.util.ArrayList at com.javacodeexamples.collections.arraylist.ArrayListSubListExample.main(ArrayListSubListExample.java:41) |
The preferred way is to always use Interface instead of casting it to the sub-class like ArrayList or LinkedList. But for any reason you want to cast List to ArrayList, you may use the below given code.
1 2 |
ArrayList<Integer> sublist = new ArrayList<Integer>( aListNumbers.subList(0, 5) ); |
Above approach uses ArrayList constructor to create a new ArrayList object from the List returned from the sublist
method.
Important points to remember:
1) Non-structural changes
Any change that does not alter the structure of the List, like modifying or replacing an element, is called non-structural change as it does not change the size of the list. The List returned by the sublist
method is backed by the original ArrayList. Any non-structural changes to the original List are also reflected in the sublist like given below.
1 2 3 4 5 6 7 8 |
System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); //replace element in the original list aListNumbers.set(0, 10); System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); |
Output
1 2 3 4 |
Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Sublist: [0, 1, 2, 3, 4] Original List: [10, 1, 2, 3, 4, 5, 6, 7, 8, 9] Sublist: [10, 1, 2, 3, 4] |
Similarly, if you make any non-structural changes to the sublist, they will also be reflected back to the original list like given below.
1 2 3 4 5 6 7 8 |
System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); //replace element in the sublist sublist.set(0, 99); System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); |
Output
1 2 3 4 |
Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Sublist: [0, 1, 2, 3, 4] Original List: [99, 1, 2, 3, 4, 5, 6, 7, 8, 9] Sublist: [99, 1, 2, 3, 4] |
2) Structural Changes
Any change that alters the structure of the List like adding a new element or removing element is a structural change as it changes the size of the list. If any structural changes are done to the sublist, they will be reflected in the original list as given below.
1 2 3 4 5 6 7 8 |
System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); //add new element in the sublist sublist.add(99); System.out.println("Original List: " + aListNumbers); System.out.println("Sublist: " + sublist); |
Output
1 2 3 4 |
Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Sublist: [0, 1, 2, 3, 4] Original List: [0, 1, 2, 3, 4, 99, 5, 6, 7, 8, 9] Sublist: [0, 1, 2, 3, 4, 99] |
Observe the position of element 99 in the original list. Since we added 99 at the end of the sublist, it is inserted after the last element of the sublist in the original list as well.
Any structural changes done to the original list directly will invalidate the sublist. If any attempt is made to access the sublist after that, it will throw ConcurrentModificationException
.
1 2 3 4 5 |
//this will invalidate the sublist aListNumbers.add(0, 10); //accessing the sublist now will result in exception System.out.println(sublist); |
Will result in,
1 2 3 4 5 6 7 8 9 |
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$SubList.checkForComodification(Unknown Source) at java.util.ArrayList$SubList.listIterator(Unknown Source) at java.util.AbstractList.listIterator(Unknown Source) at java.util.ArrayList$SubList.iterator(Unknown Source) at java.util.AbstractCollection.toString(Unknown Source) at java.lang.String.valueOf(Unknown Source) at java.io.PrintStream.println(Unknown Source) at com.javacodeexamples.collections.arraylist.ArrayListSubListExample.main(ArrayListSubListExample.java:53) |
This example is a part of the ArrayList in Java tutorial.
Please let me know your views in the comments section below.
Thank you very much. This really helped me debug my program that has been giving me a head ache. Thankyou so much
Glad it helped!