单链表程序if语句错误
Error in if statement of Singly Linked List Program
我正在编写 SinglyLinkedList
程序,我已经在其中编写了所有方法。有一个方法 deleteAtGiven(int t)
,如果变量 t 小于 0 或者 t 大于或等于大小,它会抛出异常 IndexOutOfBoundsException
但它给我一个 Unreachable 语句的错误。
我尝试将它写在 else-if 语句中并反转 else=if 语句,但它不起作用。
这是我的代码
public E deleteAtGiven(int t){
if(isEmpty()) return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}
}
它应该抛出异常。
public E deleteAtGiven(int t){
if(isEmpty()) return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size())
throw new IndexOutOfBoundsException("Invalid Position");
}
在上面的代码中,isEmpty()
检查链表是否为空。如果是这样,它 returns 一个 null
值。
如果链表不为空,则执行 if 条件下的语句。
在这里它抛出 IndexOutOfBoundsException
.
因此,else if 部分中的代码将永远不会执行,从而导致 throw 语句下面的语句无法访问。
根据我对你想要做的事情的理解,你必须这样做:
public E deleteAtGiven(int t){
if(isEmpty()) {
throw new IndexOutOfBoundsException("List is Empty");
}
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}else{
// rest logic resides here.
}
}
解释:
如果链表已经为空,则抛出空异常。否则,如果元素的索引无效,则抛出异常 "Invalid position".
如果我们更改代码的换行符和缩进以符合 Java 标准,我想您会发现问题所在:
public E deleteAtGiven(int t){
if(isEmpty())
return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}
}
throw 指令不受 if 语句控制。相反,无论是否触发 if 语句,每次调用该方法时都会抛出异常。这会导致 throw 之后的所有代码都无法访问。
你不能在 return 之后使用 throw,它将无法访问,
public E deleteAtGiven(int t){
if(isEmpty())
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size())
throw new IndexOutOfBoundsException("Invalid Position");
}
首先,如果if没有达到,你就不能做else。如果你 return 则异常将不会到达。如果你抛出异常,该函数将自动停止,所以你可以那样做
public E deleteAtGiven(int t) {
if (isEmpty())
throw new IndexOutOfBoundsException("List is Empty");
else if (t < 0 || t >= size())
throw new IndexOutOfBoundsException("Invalid Position");
else return null;
}
当它通过所有案例时,你 return null 或任何你想要的,如果没有通过案例,它将 运行 一个例外
我正在编写 SinglyLinkedList
程序,我已经在其中编写了所有方法。有一个方法 deleteAtGiven(int t)
,如果变量 t 小于 0 或者 t 大于或等于大小,它会抛出异常 IndexOutOfBoundsException
但它给我一个 Unreachable 语句的错误。
我尝试将它写在 else-if 语句中并反转 else=if 语句,但它不起作用。
这是我的代码
public E deleteAtGiven(int t){
if(isEmpty()) return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}
}
它应该抛出异常。
public E deleteAtGiven(int t){
if(isEmpty()) return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size())
throw new IndexOutOfBoundsException("Invalid Position");
}
在上面的代码中,isEmpty()
检查链表是否为空。如果是这样,它 returns 一个 null
值。
如果链表不为空,则执行 if 条件下的语句。
在这里它抛出 IndexOutOfBoundsException
.
因此,else if 部分中的代码将永远不会执行,从而导致 throw 语句下面的语句无法访问。
根据我对你想要做的事情的理解,你必须这样做:
public E deleteAtGiven(int t){
if(isEmpty()) {
throw new IndexOutOfBoundsException("List is Empty");
}
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}else{
// rest logic resides here.
}
}
解释:
如果链表已经为空,则抛出空异常。否则,如果元素的索引无效,则抛出异常 "Invalid position".
如果我们更改代码的换行符和缩进以符合 Java 标准,我想您会发现问题所在:
public E deleteAtGiven(int t){
if(isEmpty())
return null;
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size()){
throw new IndexOutOfBoundsException("Invalid Position");
}
}
throw 指令不受 if 语句控制。相反,无论是否触发 if 语句,每次调用该方法时都会抛出异常。这会导致 throw 之后的所有代码都无法访问。
你不能在 return 之后使用 throw,它将无法访问,
public E deleteAtGiven(int t){
if(isEmpty())
throw new IndexOutOfBoundsException("List is Empty");
else if (t<0 ||t>=size())
throw new IndexOutOfBoundsException("Invalid Position");
}
首先,如果if没有达到,你就不能做else。如果你 return 则异常将不会到达。如果你抛出异常,该函数将自动停止,所以你可以那样做
public E deleteAtGiven(int t) {
if (isEmpty())
throw new IndexOutOfBoundsException("List is Empty");
else if (t < 0 || t >= size())
throw new IndexOutOfBoundsException("Invalid Position");
else return null;
}
当它通过所有案例时,你 return null 或任何你想要的,如果没有通过案例,它将 运行 一个例外