TheAlgorithms/Java · error · IndexOutOfBoundsException

Index: ${index}, Size: ${size}

Error message

Index: ${index}, Size: ${size}

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java:232

            tail = newLink;
            head = tail;
        } else {
            tail.next = newLink; // currentTail(tail) --> newLink -->
            newLink.previous = tail; // currentTail(tail) <--> newLink -->
            tail = newLink; // oldTail <--> newLink(tail) -->
        }
        ++size;
    }

    /**
     * Insert an element at the index
     *
     * @param x Element to be inserted
     * @param index Index(from start) at which the element x to be inserted
     */
    public void insertElementByIndex(int x, int index, DoublyLinkedList doublyLinkedList) {
        if (index > size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        if (index == 0) {
            insertHead(x, doublyLinkedList);
        } else {
            if (index == size) {
                insertTail(x, doublyLinkedList);
            } else {
                Link newLink = new Link(x);
                Link previousLink = head; //
                for (int i = 1; i < index; i++) { // Loop to reach the index
                    previousLink = previousLink.next;
                }
                // previousLink is the Link at index - 1 from start
                previousLink.next.previous = newLink;
                newLink.next = previousLink.next;
                newLink.previous = previousLink;
                previousLink.next = newLink;
            }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate `index >= 0 && index <= doublyLinkedList.size()` before the call.
  2. Recompute size immediately before computing the index.
  3. Use insertHead/insertTail for boundary inserts to avoid index math.
  4. Synchronize access if the list is shared across threads.

Example fix

// before
dll.insertElementByIndex(x, index, dll);

// after
if (index < 0 || index > dll.size()) {
    throw new IllegalArgumentException("bad index");
}
dll.insertElementByIndex(x, index, dll);
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index <= doublyLinkedList.size()) {
    doublyLinkedList.insertElementByIndex(x, index, doublyLinkedList);
}

Try / catch

try {
    doublyLinkedList.insertElementByIndex(x, index, doublyLinkedList);
} catch (IndexOutOfBoundsException e) {
    // index beyond size
}

Prevention

When it happens

Trigger: Calling insertElementByIndex with index = size + 1 or larger. Passing an index computed from a different list's size. Index from arithmetic that overshoots.

Common situations: 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.

Related errors


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