Get Sub List of ArrayList in Java

Description

The following code shows how to get Sub List of ArrayList.

Example


// w ww  .j  a  v  a 2  s  .c om
import java.util.ArrayList;
import java.util.List;

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

    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("java2s.com");

    List lst = arrayList.subList(1, 3);
    for (int i = 0; i < lst.size(); i++)
      System.out.println(lst.get(i));

    // remove one element from sub list
    Object obj = lst.remove(0);
    System.out.println(obj + " is removed");

    for (String str: arrayList)
      System.out.println(str);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet