TheAlgorithms/Java · error · IndexOutOfBoundsException

The index must not be negative.

Error message

The index must not be negative.

What it means

Thrown by QuickSelect.select(List, int) when n < 0. The rank n represents the n-th largest element (0-based), so a negative index is meaningless. This is an IndexOutOfBoundsException to signal an invalid position rather than a general argument problem.

Source

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

     * @param n    the index
     * @param <T>  the type of list elements
     * @return the n-th largest element in the list
     * @throws IndexOutOfBoundsException if n is less than 0 or greater or equal to
     *                                   the number of elements in the list
     * @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) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate n >= 0 before calling select() and clamp or reject negative values.
  2. When converting from 1-based rank, compute n = rank - 1 only when rank >= 1.
  3. Avoid using -1 as a sentinel; use Optional or a separate flag instead.

Example fix

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

// after
if (rank < 1) throw new IllegalArgumentException("rank must be >= 1");
T nth = QuickSelect.select(items, rank - 1);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

public static boolean isValidRank(int n) { return n >= 0; }

Prevention

When it happens

Trigger: Calling select(list, -1); decrementing past zero in a loop; computing n from a subtraction that underflows; misinterpreting a 1-based rank as 0-based and subtracting 1 too many.

Common situations: Off-by-one when converting between 1-based user input and 0-based indexing; arithmetic on sizes that can go negative when lists shrink; defaulting n to -1 as an 'unset' sentinel.

Related errors


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