-1

The following code gives the error: Unreachable statement for current= current.getmNextNode(); How can it be solved?

public int indexOf(E element) {

    Node current = head;
    for (int i = 0; i < size; i++) {
        if (current.getmElement().equals(element)) ;
        {
            return i;
        }
        current = current.getmNextNode();
    }

    return -1;
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Natalie_94
  • 79
  • 3
  • 12

1 Answers1

1

You have an extra semicolon between the if statement, and what is supposed to be its body. The way you wrote it, the return i; statement will be executed regardless of the result of the condition.

public int indexOf(E element) {
  Node current = head;

  for (int i = 0; i < size; i++) {
    if (current.getmElement().equals(element)) {
      return i;
    }

    current= current.getmNextNode();
  }
  return -1;
}
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94