Get Sub List of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Get Sub List of ArrayList

Demo Code

import java.util.ArrayList;
import java.util.List;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    List lst = arrayList.subList(1,3);
       /*from   w  ww. j  a  v  a 2s . co m*/
    System.out.println("Sub list contains : ");
    for(int i=0; i< lst.size() ; i++)
      System.out.println(lst.get(i));

    Object obj = lst.remove(0);
    System.out.println(obj + " is removed from sub list");
   
    System.out.println("After removing " + obj + " from sub list, original ArrayList contains : ");
    for(int i=0; i< arrayList.size() ; i++)
      System.out.println(arrayList.get(i));
  }
}

Result


Related Tutorials