TheAlgorithms/Java · error · IndexOutOfBoundsException

The index must be less than the number of elements.

Error message

The index must be less than the number of elements.

What it means

Thrown by QuickSelect.select(List, int) when n >= list.size(). Since n is a 0-based rank, the maximum valid value is size() - 1; any larger value selects beyond the last element. This is an IndexOutOfBoundsException because it is a positional precondition violation.

Source

Thrown at src/main/java/com/thealgorithms/searches/QuickSelect.java:46

     * @throws IllegalArgumentException  if the list is empty
     * @throws NullPointerException      if {@code list} is null
     */
    public static <T extends Comparable<T>> T select(List<T> list, int n) {
        Objects.requireNonNull(list, "The list of elements must not be null.");

        if (list.isEmpty()) {
            String msg = "The list of elements must not be empty.";
            throw new IllegalArgumentException(msg);
        }

        if (n < 0) {
            String msg = "The index must not be negative.";
            throw new IndexOutOfBoundsException(msg);
        }

        if (n >= list.size()) {
            String msg = "The index must be less than the number of elements.";
            throw new IndexOutOfBoundsException(msg);
        }

        int index = selectIndex(list, n);
        return list.get(index);
    }

    private static <T extends Comparable<T>> int selectIndex(List<T> list, int n) {
        return selectIndex(list, 0, list.size() - 1, n);
    }

    private static <T extends Comparable<T>> int selectIndex(List<T> list, int left, int right, int n) {
        while (true) {
            if (left == right) {
                return left;
            }
            int pivotIndex = pivot(list, left, right);
            pivotIndex = partition(list, left, right, pivotIndex, n);
            if (n == pivotIndex) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate 0 <= n < list.size() before calling select().
  2. When accepting a 1-based rank r, pass r - 1 and ensure r <= list.size().
  3. Recompute list.size() immediately before the call if the list may be mutated concurrently.

Example fix

// before
T nth = QuickSelect.select(items, rank);

// after
if (rank >= items.size()) throw new IllegalArgumentException("rank out of range");
T nth = QuickSelect.select(items, rank);
Defensive patterns

Strategy: validation

Validate before calling

if (n >= list.size()) throw new IllegalArgumentException("n must be < size, got " + n);

Type guard

public static <T> boolean isWithinBounds(List<T> list, int n) { return n >= 0 && n < list.size(); }

Prevention

When it happens

Trigger: Calling select(list, list.size()); using a rank equal to the count instead of count - 1; computing n from a stale size after the list shrank; passing a 1-based rank directly without subtracting 1.

Common situations: Mixing up 1-based 'k-th largest' semantics with 0-based indexing; size computed before a concurrent removal; user input not bounds-checked.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/af8bb96f2e2e2781. Report an issue: GitHub.