How to get sublist of an ArrayList

use this link

resource tips for sending first message online dating How to get sublist of an ArrayList

This can be achieve using the subList() method of ArrayList class.

Syntax: List subList(int fromIndex, int toIndex)

Here fromIndex is inclusive and toIndex is exclusive.


  1. The como conocer a hombres millonarios subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example.
  2. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).
  3. The subList method throws IndexOutOfBoundsException– if the specified indexes are out of the range of ArrayList (fromIndex < 0 || toIndex > size).
  4. IllegalArgumentException– if the starting index is greater than the end point index (fromIndex > toIndex).

learn this here now Example:

package ArraylistClass;
import java.util.ArrayList;
import java.util.List;

public class SublistExample {

  public static void main(String[] args) {
    ArrayList<String> al = new ArrayList<String>();

       //Addition of elements in ArrayList
    al.add("Node JS");

       System.out.println("Original ArrayList Content: "+al);

       //Sublist to ArrayList
       ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4));
       System.out.println("SubList stored in ArrayList: "+al2);

       //Sublist to List
       List<String> list = al.subList(1, 4);
       System.out.println("SubList stored in List: "+list);

} Output:

Original ArrayList Content: [C++, Java, C#, SQL, DotNet, Node JS]
SubList stored in ArrayList: [Java, C#, SQL]
SubList stored in List: [Java, C#, SQL]