{"record":{"id":"a2051d3c48bc50c5","repo":"TheAlgorithms/Java","slug":"index-index-size-size-a2051d","errorCode":null,"errorMessage":"Index: ${index}, Size: ${size}","messagePattern":"Index: (.+?), Size: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java","lineNumber":232,"sourceCode":"            tail = newLink;\n            head = tail;\n        } else {\n            tail.next = newLink; // currentTail(tail) --> newLink -->\n            newLink.previous = tail; // currentTail(tail) <--> newLink -->\n            tail = newLink; // oldTail <--> newLink(tail) -->\n        }\n        ++size;\n    }\n\n    /**\n     * Insert an element at the index\n     *\n     * @param x Element to be inserted\n     * @param index Index(from start) at which the element x to be inserted\n     */\n    public void insertElementByIndex(int x, int index, DoublyLinkedList doublyLinkedList) {\n        if (index > size) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + size);\n        }\n        if (index == 0) {\n            insertHead(x, doublyLinkedList);\n        } else {\n            if (index == size) {\n                insertTail(x, doublyLinkedList);\n            } else {\n                Link newLink = new Link(x);\n                Link previousLink = head; //\n                for (int i = 1; i < index; i++) { // Loop to reach the index\n                    previousLink = previousLink.next;\n                }\n                // previousLink is the Link at index - 1 from start\n                previousLink.next.previous = newLink;\n                newLink.next = previousLink.next;\n                newLink.previous = previousLink;\n                previousLink.next = newLink;\n            }","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java#L214-L250","documentation":"Thrown by DoublyLinkedList.insertElementByIndex(int x, int index, DoublyLinkedList) when index > size. Insertion is permitted at index 0 (head) through index size (tail), so the valid window is [0, size]. The guard catches an index that points beyond the tail.","triggerScenarios":"Calling insertElementByIndex with index = size + 1 or larger. Passing an index computed from a different list's size. Index from arithmetic that overshoots.","commonSituations":"Index derived from another collection's length. Stale size assumption after the list grew or shrank. Off-by-one using `<` where the loop produced an index one past size.","solutions":["Validate `index >= 0 && index <= doublyLinkedList.size()` before the call.","Recompute size immediately before computing the index.","Use insertHead/insertTail for boundary inserts to avoid index math.","Synchronize access if the list is shared across threads."],"exampleFix":"// before\ndll.insertElementByIndex(x, index, dll);\n\n// after\nif (index < 0 || index > dll.size()) {\n    throw new IllegalArgumentException(\"bad index\");\n}\ndll.insertElementByIndex(x, index, dll);","handlingStrategy":"validation","validationCode":"if (index >= 0 && index <= doublyLinkedList.size()) {\n    doublyLinkedList.insertElementByIndex(x, index, doublyLinkedList);\n}","typeGuard":null,"tryCatchPattern":"try {\n    doublyLinkedList.insertElementByIndex(x, index, doublyLinkedList);\n} catch (IndexOutOfBoundsException e) {\n    // index beyond size\n}","preventionTips":["Remember valid insert range is [0, size] inclusive of tail.","Recompute size before index math.","Use insertHead/insertTail at boundaries to avoid index errors."],"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"}