Java Collection Tutorial - Java ArrayList.subList(int fromIndex, int toIndex)








Syntax

ArrayList.subList(int fromIndex, int toIndex) has the following syntax.

public List <E> subList(int fromIndex,  int toIndex)

Example

In the following code shows how to use ArrayList.subList(int fromIndex, int toIndex) method.

/*from   w  w  w. j a  v  a2  s . co  m*/
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String args[]) {
    ArrayList<Integer>  arrlist = new ArrayList<Integer> (5);
  
    arrlist.add(1);
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    System.out.println(arrlist);

    List<Integer>  arrlist2 = arrlist.subList(2, 4);

    System.out.println(arrlist2);
    
  }
}

The code above generates the following result.