{"record":{"id":"479ae4f4fda4d61f","repo":"TheAlgorithms/Java","slug":"position-out-of-bounds","errorCode":null,"errorMessage":"Position out of bounds","messagePattern":"Position out of bounds","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java","lineNumber":109,"sourceCode":"                sb.append(\", \");\n            }\n            current = current.next;\n        }\n        sb.append(\" ]\");\n        return sb.toString();\n    }\n\n    /**\n     * Removes and returns the element at the specified position in the list.\n     * Throws an IndexOutOfBoundsException if the position is invalid.\n     *\n     * @param pos the position of the element to remove\n     * @return the value of the removed element\n     * @throws IndexOutOfBoundsException if the position is out of range\n     */\n    public E remove(int pos) {\n        if (pos >= size || pos < 0) {\n            throw new IndexOutOfBoundsException(\"Position out of bounds\");\n        }\n\n        Node<E> before = head;\n        for (int i = 1; i <= pos; i++) {\n            before = before.next;\n        }\n        Node<E> destroy = before.next;\n        E saved = destroy.value;\n        before.next = destroy.next;\n\n        if (destroy == tail) {\n            tail = before;\n        }\n        destroy = null;\n        size--;\n        return saved;\n    }\n}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java#L91-L127","documentation":"Thrown by CircleLinkedList.remove(int pos) when pos >= size or pos < 0. The list uses 0-based logical positions and the guard validates the index before walking the node chain. An invalid pos would dereference a null node pointer.","triggerScenarios":"Calling remove(size) (off-by-one treating size as a valid index). Calling remove on an empty list (size=0, any non-negative pos fails). Passing a negative index.","commonSituations":"Loop `for (int i = 0; i <= list.size(); i++) remove(i)` including the size boundary. Removing by an index from a parallel array sized differently. Negative index from an arithmetic underflow.","solutions":["Validate `pos >= 0 && pos < list.size()` before remove.","Guard for empty list separately.","Use `i < list.size()` (strict) in removal loops, and recompute size each iteration since remove shrinks it.","When removing multiple elements, decrement indices or iterate in reverse."],"exampleFix":"// before\nfor (int i = 0; i <= list.size(); i++) list.remove(i);\n\n// after\nwhile (!list.isEmpty()) {\n    list.remove(0);\n}","handlingStrategy":"validation","validationCode":"if (pos >= 0 && pos < list.size()) {\n    return list.remove(pos);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return list.remove(pos);\n} catch (IndexOutOfBoundsException e) {\n    // invalid pos — skip\n}","preventionTips":["Use strict `<` against size in loops, never `<=`.","Recompute size each iteration when removing in a loop.","Iterate in reverse when removing multiple indexed elements."],"tags":["linked-list","index-out-of-bounds","data-structure","off-by-one","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}