Skip to content

IndexTreeList java.lang.IndexOutOfBoundsException calling addFirst(E e) on an empty list #1059

@boiten

Description

@boiten

Issue Summary: The method checkIndex(index: Int) in the class IndexTreeList (version 3.1.0) appears to enforce an index boundary check that aligns with the contract for AbstractList.set(index, element), but not with that of add(E e) or addFirst(E e).

Code Reference:

fun checkIndex(index: Int) {
    if (index < 0 || index >= size)
        throw IndexOutOfBoundsException()
}

According to the Java documentation, the index validation for add(int index, E element) (and by extension for similar insertion methods such as addFirst(E e)) should be:

IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size())

The issue manifests when calling addFirst(E e) on an empty IndexTreeList. Internally, this results in a call to checkIndex(0), which throws IndexOutOfBoundsException, even though 0 is a valid index for insertion into an empty list (where size == 0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions