bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java ArrayList subList() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java ArrayList subList() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.ArrayList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The subList() method returns a new list (referred to as a sublist ) which contains the items of the list between two indices.
Definition and Usage
Java ArrayList subList() Method

❮ ArrayList Methods

Example

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println( cars.subList(1, 3) );
  }
}

Definition and Usage

The subList() method returns a new list (referred to as a sublist ) which contains the items of the list between two indices.

Note

The item at the last index is not included in the sublist.

Note

The sublist is a view of the original list, which means that changing the sublist also changes the original list.

Syntax

public List sublist(int
start , int
end )

Parameter Values

ParameterDescription
startRequired. The index where the sublist starts.
endRequired. The index where the sublist ends. The item at this position is not included in the sublist.

Technical Details

Returns:A new List containing elements of the list.
Throws:IndexOutOfBoundsException - If one of the indices is less than zero or greater than the size of the list. IllegalArgumentException - If end index is less than the start index.

Previous

Java ArrayList spliterator() Method

Next

Java ArrayList toArray() Method