Check out example codes for "ArrayList subList(int fromIndex, int toIndex) method in java". It will help you in understanding the concepts better.

Code Example 1

import java.util.ArrayList;
import java.util.List;
public class ArrayListSubListMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<String> al = new ArrayList<String>();
         al.add("orange");
         al.add("apple");
         al.add("strawberry");
         al.add("banana");
         al.add("mango");
         System.out.println("Given ArrayList: " + al);
         // get subList using subList() method
         System.out.println("End point indices are out of order (fromIndex > toIndex): ");
         List<String> li = al.subList(6, 2);
         // printing subList
         System.out.println("Sublist of ArrayList: " + li);
      }
      catch(IndexOutOfBoundsException e)
      {
         System.out.println("Exception: " + e);
      }
      catch(IllegalArgumentException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}

Code Example 2

import java.util.ArrayList;
import java.util.List;
public class ArrayListSubListMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<String> al = new ArrayList<String>();
         al.add("orange");
         al.add("apple");
         al.add("strawberry");
         al.add("banana");
         al.add("mango");
         System.out.println("Given ArrayList: " + al);
         // get subList using subList() method
         List<String> li = al.subList(2, 4);
         // printing subList
         System.out.println("Sublist of ArrayList: " + li);
      }
      catch(IndexOutOfBoundsException e)
      {
         System.out.println("Exception: " + e);
      }
      catch(IllegalArgumentException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}

Code Example 3

import java.util.ArrayList;
import java.util.List;
public class ArrayListSubListMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<String> al = new ArrayList<String>();
         al.add("orange");
         al.add("apple");
         al.add("strawberry");
         al.add("banana");
         al.add("mango");
         System.out.println("Given ArrayList: " + al);
         // get subList using subList() method
         System.out.println("End index value is out of range: ");
         List<String> li = al.subList(2, 6);
         // printing subList
         System.out.println("Sublist of ArrayList: " + li);
      }
      catch(IndexOutOfBoundsException e)
      {
         System.out.println("Exception: " + e);
      }
      catch(IllegalArgumentException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}

Learn ReactJs, React Native from akashmittal.com