{"record":{"id":"c11a67de71a1034b","repo":"TheAlgorithms/Java","slug":"the-index-must-not-be-negative","errorCode":null,"errorMessage":"The index must not be negative.","messagePattern":"The index must not be negative\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/QuickSelect.java","lineNumber":41,"sourceCode":"     * @param n    the index\n     * @param <T>  the type of list elements\n     * @return the n-th largest element in the list\n     * @throws IndexOutOfBoundsException if n is less than 0 or greater or equal to\n     *                                   the number of elements in the list\n     * @throws IllegalArgumentException  if the list is empty\n     * @throws NullPointerException      if {@code list} is null\n     */\n    public static <T extends Comparable<T>> T select(List<T> list, int n) {\n        Objects.requireNonNull(list, \"The list of elements must not be null.\");\n\n        if (list.isEmpty()) {\n            String msg = \"The list of elements must not be empty.\";\n            throw new IllegalArgumentException(msg);\n        }\n\n        if (n < 0) {\n            String msg = \"The index must not be negative.\";\n            throw new IndexOutOfBoundsException(msg);\n        }\n\n        if (n >= list.size()) {\n            String msg = \"The index must be less than the number of elements.\";\n            throw new IndexOutOfBoundsException(msg);\n        }\n\n        int index = selectIndex(list, n);\n        return list.get(index);\n    }\n\n    private static <T extends Comparable<T>> int selectIndex(List<T> list, int n) {\n        return selectIndex(list, 0, list.size() - 1, n);\n    }\n\n    private static <T extends Comparable<T>> int selectIndex(List<T> list, int left, int right, int n) {\n        while (true) {\n            if (left == right) {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/QuickSelect.java#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate n >= 0 before calling select() and clamp or reject negative values.","When converting from 1-based rank, compute n = rank - 1 only when rank >= 1.","Avoid using -1 as a sentinel; use Optional or a separate flag instead."],"exampleFix":"// before\nT nth = QuickSelect.select(items, rank - 1);\n\n// after\nif (rank < 1) throw new IllegalArgumentException(\"rank must be >= 1\");\nT nth = QuickSelect.select(items, rank - 1);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"n must be >= 0, got \" + n);","typeGuard":"public static boolean isValidRank(int n) { return n >= 0; }","tryCatchPattern":null,"preventionTips":["Never use -1 as an 'unset rank' sentinel; use Optional or a flag.","When converting from 1-based, subtract only after confirming rank >= 1.","Guard decrementing loops so n never drops below zero."],"tags":["selection","index-out-of-bounds","off-by-one","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}