YouTip LogoYouTip

Java Arraylist Sublist

Java ArrayList subList() Method |

-- Learning not just technology, but dreams!

Java Tutorial

Java Object-Oriented Programming

Java Advanced Tutorial

Java Collections Framework

Java LinkedList

Java ArrayList subList() Method

Java ArrayList Java ArrayList

The subList() method is used to extract and return a part of the ArrayList.

The syntax of the subList() method is:

arraylist.subList(int fromIndex, int toIndex)

Note: arraylist is an object of the ArrayList class.

Parameter Description:

  • fromIndex - The starting position of the elements to be extracted, including the element at this index position.
  • toIndex - The ending position of the elements to be extracted, excluding the element at this index position.

Return Value

Returns the extracted part of the given ArrayList.

If fromIndex is less than 0 or greater than the length of the array, an IndexOutOfBoundsException is thrown.

If the value of fromIndex is greater than toIndex, an IllegalArgumentException is thrown.

Note: The ArrayList contains elements starting from the fromIndex position until the element index position is toIndex-1, and the element at index position toIndex is not included.

subList illustration

Example

Extract a sublist from an ArrayList:

Example

import java.util.ArrayList;

class Main {
    public static void main(String[] args){
        // Create an ArrayList
        ArrayList<String> sites = new ArrayList<>();
        sites.add("Google");
        sites.add("");
        sites.add("Taobao");
        sites.add("Wiki");

        System.out.println("Website List: " + sites);

        // Elements from position 1 to 3
        System.out.println("SubList: " + sites.subList(1, 3));
    }
}

Output of the above program:

Website List: [Google, , Taobao, Wiki]
SubList: [, Taobao]

In the example above, we used the subList() method to get elements from index 1 to 3 (excluding 3).

Split an ArrayList into two ArrayLists:

Example

import java.util.ArrayList;

class Main {
    public static void main(String[] args){
        // Create an array
        ArrayList<Integer> ages = new ArrayList<>();

        // Add some elements to the array
        ages.add(10);
        ages.add(12);
        ages.add(15);
        ages.add(19);
        ages.add(23);
        ages.add(34);

        System.out.println("Age List: " + ages);

        // Under 18
        System.out.println("Under 18: " + ages.subList(0, 3));

        // Over 18
        System.out.println("Over 18: " + ages.subList(3, ages.size()));
    }
}

Output of the above program:

Age List: [10, 12, 15, 19, 23, 34]
Under 18: [10, 12, 15]
Over 18: [19, 23, 34]

In the example above, we created an array named ages. The subList() method split this array into two arrays: ages under 18 and ages over 18.

Please note that we used the ages.size() method to get the length of the array. To learn more about the size() method, visit Java ArrayList size().

Java ArrayList Java ArrayList

← Python3 Att List CopyPython3 Att List Remove β†’