-2
public void delete(int index)
{
    if (index < 0 || index > _____)
    {
        throw new ArrayIndexOutOfBoundsException();
    }

    {
        Node<X> current = head;
        for (int i = 0; i < index; i++)
        {
            current = current.getLink();
        }

        Node<X> newNode = new Node<X>();
        newNode.setLink(current.getLink());
        current.setLink(newNode);
    }
}

So I can't seem to figure out the logic in writing an outofbounds exception for index when using node objects. It's not like i can do head.length, so would i use a .getLink loop first to find the Null? It's not like I can do index != null either.

zessx
  • 68,042
  • 28
  • 135
  • 158
Jakkie Chan
  • 317
  • 4
  • 14

2 Answers2

2
if (index < 0 || index >= sizeOfList)
{
    throw new ArrayIndexOutOfBoundsException();
}

If you have a function that computes the length of the collection.

Rey Libutan
  • 5,226
  • 9
  • 42
  • 73
1

You need to check for the edge cases, while writing a code. That's the best practice.

Since, you missed the edge cases, of size 0 and list full, in the mentioned example, you were getting ArrayIndexOutOfBoundsException.