ArrayList subList() method in java

Let’s learn ArrayList subList(int fromIndex, int toIndex) method in java.

ArrayList subList(int fromIndex, int toIndex) method in java

subList(int fromIndex, int toIndex) method of ArrayList class returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list isempty.)

Syntax:

public List subList(int fromIndex, int toIndex)

Parameters:

fromIndex low endpoint (inclusive) of the subList.

toIndex high endpoint (exclusive) of the subList.

Returns:

a view of the specified range within this list.

Throws:

IndexOutOfBoundsException – for an illegal endpoint index value(fromIndex < 0 || toIndex > size ||fromIndex > toIndex).

IllegalArgumentException – if the endpoint indices are out of order (fromIndex > toIndex).

Now let’s see example on ArrayList subList(int fromIndex, int toIndex) method.

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);
      }
   }
}

Output:

Given ArrayList: [orange, apple, strawberry, banana, mango]
Sublist of ArrayList: [strawberry, banana]


Let’s see example on ArrayList subList() method for IndexOutOfBoundsException.

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);
      }
   }
}

Output:

Given ArrayList: [orange, apple, strawberry, banana, mango]
End index value is out of range:
Exception: java.lang.IndexOutOfBoundsException: toIndex = 6


Now let’s see example on ArrayList subList() method for IllegalArgumentException.

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);
      }
   }
}

Output:

Given ArrayList: [orange, apple, strawberry, banana, mango]
End point indices are out of order (fromIndex > toIndex):
Exception: java.lang.IllegalArgumentException: fromIndex(6) > toIndex(2)


Also read – major features of java