package LinkedList;
public class LinkedList {
public class Node {
Object data;
Node next;
//Constructor of Node
Node(Object data){
this.data = data;
}
//getter
Object getdata(){
return this.data;
}
Node getnext(){
return this.next;
}
//setter
void setnext(Node n){
this.next = n;
}
}
Node header = null;
int size = 0;
//Constructor of LinkedList
public LinkedList(){};
//return size
int size(){
return size;
}
//return that list is empty or not
boolean isEmpty(){
if (size != 0){
return false;
}
return true;
}
Object first(){
return header.getdata();
}
Object Last(int size){
Node c;
for(int i=0 ;i<size-1 ;i++){
c = header.getnext();
if (i == size-2){
Object returndata = c.getdata();
return returndata;
}
}
}
}
first()
函数在 eclipse 上没有任何错误。
但在 last()
函数中,我收到错误,该方法必须返回 Object 类型的结果。如何解决这个错误?
最佳答案
问题是 Last() 并不总是返回一个值,尽管它声称这样做。每个代码路径都必须产生返回。
Object Last(int size){
Node c;
for(int i=0 ;i<size-1 ;i++){
c = header.getnext();
if (i == size-2){
Object returndata = c.getdata();
return returndata;
}
}
return null;
}
关于java - 该方法必须返回 Object 类型的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092748/