How to get sublist from ArrayList in Java ?

In this tutorial, we will learn about How to get sublist from ArrayList in Java ?

ArrayList in Java is a resizable-array implementation of the List interface. ArrayList in Java implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Sometimes we may need to get sublist from Arraylist in Java. ArrayList class in Java provides sublist() method to get sublist from ArrayList. This method returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. If the fromIndex and toIndex are equal, then the returned list is empty.

Syntax of sublist() method:

fromIndex- sublist containing elements from start index inclusive.
toIndex- sublist containing elements till end index exclusive.

a sublist.

IndexOutOfBoundsException – if an index value is out of range (fromIndex < 0 || toIndex > size)
IllegalArgumentException – if the indices are out of order (fromIndex > toIndex)




Related posts

Leave a Comment