{"record":{"id":"af8bb96f2e2e2781","repo":"TheAlgorithms/Java","slug":"the-index-must-be-less-than-the-number-of-elements","errorCode":null,"errorMessage":"The index must be less than the number of elements.","messagePattern":"The index must be less than the number of elements\\.","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/QuickSelect.java","lineNumber":46,"sourceCode":"     * @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) {\n                return left;\n            }\n            int pivotIndex = pivot(list, left, right);\n            pivotIndex = partition(list, left, right, pivotIndex, n);\n            if (n == pivotIndex) {","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/QuickSelect.java#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"Mixing up 1-based 'k-th largest' semantics with 0-based indexing; size computed before a concurrent removal; user input not bounds-checked.","solutions":["Validate 0 <= n < list.size() before calling select().","When accepting a 1-based rank r, pass r - 1 and ensure r <= list.size().","Recompute list.size() immediately before the call if the list may be mutated concurrently."],"exampleFix":"// before\nT nth = QuickSelect.select(items, rank);\n\n// after\nif (rank >= items.size()) throw new IllegalArgumentException(\"rank out of range\");\nT nth = QuickSelect.select(items, rank);","handlingStrategy":"validation","validationCode":"if (n >= list.size()) throw new IllegalArgumentException(\"n must be < size, got \" + n);","typeGuard":"public static <T> boolean isWithinBounds(List<T> list, int n) { return n >= 0 && n < list.size(); }","tryCatchPattern":null,"preventionTips":["Recompute list.size() immediately before the call if the list may change.","For 1-based ranks r, pass r - 1 only after confirming r <= list.size().","Bounds-check at the API boundary and fail fast with a clear message."],"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"}