ArrayList subList() method in Java with Examples

subList() method is used to get a portion of the ArrayList and in this post, we are going to discuss the subList() method in detail.

  • Method declaration – public List<E> subList(int fromIndex, int toIndex)
  • What does it do? It will take two arguments – fromIndex and toIndex. fromIndex ( inclusive ) is the index from where we will start considering our subList and toIndex ( exclusive ) is the index up to which we will consider our list but toIndex itself would not be considered in the subList. So, our sublist will start from fromIndex and end at toIndex-1. Here the indexing is 0-based, which means, the 1st element is considered at the 0th index
  • What does it return? It will return a new list starting from fromIndex to toIndex-1
Code Example
public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");
		al.add("fourth");
		al.add("fifth");

		System.out.println("ArrayList contents: " + al);

		List<String> subList = al.subList(1, 4);

		System.out.println("subList contents: " + subList);

	}
}

Output –

ArrayList contents: [first, second, third, fourth, fifth]
subList contents: [second, third, fourth]

Here, we passed fromIndex as 1 and toIndex as 4. So, our subList was considered from 1 ( inclusive ) to 4 ( exclusive ) and thus returned elements present at indexes 1,2, and 3.

The What If scenarios

What if fromIndex = toIndex?

In this case, it will return an empty sublist as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");
		al.add("fourth");
		al.add("fifth");

		System.out.println("ArrayList contents: " + al);

		// fromIndex = 1, and toIndex = 1
		List<String> subList = al.subList(1, 1);

		System.out.println("subList contents: " + subList);

	}
}

Output –

ArrayList contents: [first, second, third, fourth, fifth]
subList contents: []
What if fromIndex > toIndex?

In this scenario, the set() method will throw a IllegalArgumentException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");
		al.add("fourth");
		al.add("fifth");

		System.out.println("ArrayList contents: " + al);

		// fromIndex = 2, and toIndex = 1
		List<String> subList = al.subList(2, 1);

		System.out.println("subList contents: " + subList);

	}
}

Output –

ArrayList contents: [first, second, third, fourth, fifth]
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(2) > toIndex(1)
	at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
	at java.base/java.util.ArrayList.subList(ArrayList.java:1108)
What if fromIndex<0 or toIndex>=size of the list?

Here, the set() method will throw an IndexOutOfBoundsException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al.add("first");
		al.add("second");
		al.add("third");
		al.add("fourth");
		al.add("fifth");

		System.out.println("ArrayList contents: " + al);

		// fromIndex = -1, and toIndex = 1
		List<String> subList = al.subList(-1, 1);

		System.out.println("subList contents: " + subList);

	}
}

Output –

ArrayList contents: [first, second, third, fourth, fifth]
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1
	at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:505)
	at java.base/java.util.ArrayList.subList(ArrayList.java:1108)

What if we used the set() method on a null ArrayList?

Now, it will throw a NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();

		al = null;
		List<String> subList = al.subList(1, 1);

		System.out.println("subList contents: " + subList);

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.subList(int, int)" because "al" is null

Please visit this link if you want to know more about the ArrayList class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *