Sublist 0 0 java

The ArrayList.subList method returns a view/portion of the original list between fromIndex [included] and toIndex [not included].

Syntax

arrayListObj.subList[int fromIndex, int toIndex];
  • fromIndex: Index from which the sublist starts. If fromIndex is less than 0, then IndexOutOfBoundsException will be thrown. If the fromIndex is greater than toIndex, then IllegalArgumentException will be thrown.

  • toIndex: Index at which the sublist ends. The element at the endIndex will not be considered. If toIndex is greater than ArrayList size then, IndexOutOfBoundsException will be thrown.

  • The subList method returns a portion of the ArrayList object as a List object.

import java.util.ArrayList; import java.util.List; class GetSubList { public static void main[String[] args] { ArrayList strList = new ArrayList[]; strList.add["E"]; strList.add["D"]; strList.add["U"]; strList.add["C"]; strList.add["T"]; strList.add["I"]; strList.add["V"]; strList.add["E"]; System.out.println["The list is " + strList]; List strListCopy = strList.subList[0, strList.size[]]; // returns complete list System.out.println["\nGetting complete list by calling strList.sublist[0, strList.size[]] \n" + strListCopy]; List firstThreeStr = strList.subList[0, 3]; System.out.println["\nGetting first 3 letters of the list by calling strList.sublist[0, 3] \n" + firstThreeStr ]; // returns first 3 elements of the list } }

In the code above, we have created a strList ArrayList with some elements.

First, we set the fromIndex as 0 and toIndex as the size of the list to get the complete list. The subList will return a list of elements from index 0 to the index list size -1.

strList.subList[0, strList.size[]]

Then, we get the first three elements of the strList by calling:

strList.subList[0, 3];

This will extract elements from index 0 to [3 - 1] . Therefore, the elements at index 0, 1, and 2 will be returned.

The syntax of the subList[] method is:

arraylist.subList[int fromIndex, int toIndex]

Here, arraylist is an object of the ArrayList class.

subList[] Parameters

The subList[] method takes two parameters.

  • fromIndex - the starting position from where elements are extracted
  • toIndex - the ending position up to which elements are extracted

subList[] Return Value

  • returns a portion of arraylist from the given arraylist
  • throws IndexOutOfBoundsException, if fromIndex is less than 0 or toIndex is greater than the size of arraylist
  • throws IllegalArgumentException, if fromIndex is greater than toIndex.

Note: The portion of arraylist contains elements starting at fromIndex and extends up to element at toIndex-1. That is, the element at toIndex is not included.

Working of ArrayList subList[]

Example 1: Get a Sub List From an ArrayList

import java.util.ArrayList; class Main { public static void main[String[] args] { // create an ArrayList ArrayList languages = new ArrayList[]; // add some elements to the ArrayList languages.add["JavaScript"]; languages.add["Java"]; languages.add["Python"]; languages.add["C"]; System.out.println["ArrayList: " + languages]; // element from 1 to 3 System.out.println["SubList: " + languages.subList[1, 3]]; } }

Output

ArrayList: [JavaScript, Java, Python, C] SubList: [Java, Python]

In the above example, we have used the subList[] method to get elements from index 1 to 3 [excluding 3].

Note: If you want to know how to get the index of the specified element, visit Java ArrayList indexOf[].

Example 2: Split a Single ArrayList into Two ArrayLists

import java.util.ArrayList; class Main { public static void main[String[] args] { // create an ArrayList ArrayList ages = new ArrayList[]; // add some elements to the ArrayList ages.add[10]; ages.add[12]; ages.add[15]; ages.add[19]; ages.add[23]; ages.add[34]; System.out.println["List of Age: " + ages]; // ages below 18 System.out.println["Ages below 18: " + ages.subList[0, 3]]; // ages above 18 System.out.println["Ages above 18: " + ages.subList[3, ages.size[]]]; } }

Output

List of Age: [10, 12, 15, 19, 23, 34] Ages below 18: [10, 12, 15] Ages above 18: [19, 23, 34]

In the above example, we have created an arraylist named ages. Here, we have used the subList[] method to split the arraylist into two arraylists: Ages below 18 and Ages above 18.

Note that we have used the ages.size[] method to get the length of the arraylist. To learn more on the size[] method, visit Java ArrayList size[].

Learn how to get a sublist of an existing ArrayList. We will be using ArrayList.subList[] method to get the sublist of arraylist object.

1. ArrayList.subList[] method

This method returns a view of the portion of this list between the specified fromIndex [inclusive] and toIndex [exclusive].

1.1. subList[] Method Syntax

public List subList[int fromIndex, int toIndex] { subListRangeCheck[fromIndex, toIndex, size]; return new SubList[this, 0, fromIndex, toIndex]; }

1.2. subList[] Method Parameters

fromIndex – start index in existing arraylist. It is inclusive.
toIndex – last index in existing arraylist. It is exclusive.

Please note that any change made on objects in sublist will be reflected on original arraylist as well.

2. Get sublist of arraylist example

Java program to get a sublist of arraylist from an existing sublist. We are getting the sublist from index 2 to 6.

Please note that arraylist index starts from 0.

import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList list = new ArrayList[Arrays.asList[0,1,2,3,4,5,6,7,8,9]]; ArrayList sublist = new ArrayList[ list.subList[2, 6] ]; System.out.println[sublist]; } }

Program output.

[2, 3, 4, 5]

If we want to get sublist from specified index to end of list, then pass the length of arraylist in second argument of method.

import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] { ArrayList list = new ArrayList[Arrays.asList[0,1,2,3,4,5,6,7,8,9]]; System.out.println[list.subList[2, list.size[]]]; } }

Program output.

[2, 3, 4, 5, 6, 7, 8, 9]

3. Remove sublist of arraylist example

When we have sublist view of arraylist, we can use this sublist to remove multiple items from arraylist also.

Program output.

[0, 1, 6, 7, 8, 9]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Let us know if you liked the post. That’s the only way we can improve.

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 al = new ArrayList[]; 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 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 al = new ArrayList[]; 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 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

Video liên quan

Chủ Đề