Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用递归方法的链表获取方法_Java - Fatal编程技术网

Java 使用递归方法的链表获取方法

Java 使用递归方法的链表获取方法,java,Java,我试图为链表设计一个get方法。 它将int位置作为参数,并返回给定位置的列表元素(位置从零开始) 我认为我的逻辑是正确的,但没有编译。有人能指出我在这里做错了什么吗 abstract public class AbstractListNode { abstract public Object first ( ); abstract public AbstractListNode rest ( ); abstract public boolean isEmpty ( )

我试图为链表设计一个get方法。 它将int位置作为参数,并返回给定位置的列表元素(位置从零开始)

我认为我的逻辑是正确的,但没有编译。有人能指出我在这里做错了什么吗

abstract public class AbstractListNode {

    abstract public Object first ( );
    abstract public AbstractListNode rest ( );
    abstract public boolean isEmpty ( );
    abstract public int size( );
    abstract public Object get(int index);
    // Every other list-processing method goes here.
}

class NonemptyListNode extends AbstractListNode {

    private Object myFirst;
    private AbstractListNode myRest;

    // cons in Scheme.
    public NonemptyListNode (Object first, AbstractListNode rest) {
        myFirst = first;
        if (rest == null) {
        myRest = new EmptyListNode ( );
        } else {
        myRest = rest;
        }
    }

    public NonemptyListNode (Object first) {
        this (first, new EmptyListNode ( ));
    }

    // car in Scheme.
    public Object first ( ) {
        return myFirst;
    }

    // cdr in Scheme.
    public AbstractListNode rest ( ) {
        return myRest;
    }

    public boolean isEmpty ( ) {
    return false;
    }

    public int size ( ) {
        return 1+myRest.size();
    }

    public Object get(int index){
        if(index+1 > this.size())
            throw new IllegalArgumentException ("Out of Range");
        else if(index == 0){
            return myFirst;
        }
        else{
            index = index-1;
            AbstractListNode l = this.myRest;
            l.get(index);
        }          
    }
}

class EmptyListNode extends AbstractListNode {

    public EmptyListNode ( ) {

    }

    public Object first ( ) {
        throw new IllegalArgumentException ("There is no 'first' value stored in an EmptyListNode.");
    }

    public AbstractListNode rest ( ) {
        throw new IllegalArgumentException ("No elements follow an EmptyListNode.");
    }

    public boolean isEmpty ( ) {
        return true;
    }

    public int size( ) {
        return 0;
    }

    public Object get(int index){
        throw new IllegalArgumentException ("Out of Range");
    }
}
我在NonemptyListNode中的get方法中出错

错误是您没有返回语句:

public Object get(int index){
    if(index+1 > this.size())
        throw new IllegalArgumentException ("Out of Range");
    else if(index == 0){
        return myFirst;
    }
    else{
        index = index-1;
        AbstractListNode l = this.myRest;
        l.get(index);
        /*
         * here you should have a return statement, if this else block 
         * gets executed, no returns will be found. 
         */   
    }          
}

如果你得到一个编译错误,你真的应该说它是什么,它在哪里出现。。。这样我们就不必复制你的全部代码并自己编译。我认为我的逻辑是正确的,但编译时编译器不会准确地告诉你哪一行和哪一列有错误,以及你的代码有哪些错误,所以请将它粘贴在这里。对此我深表歉意。我在NonemptyListNode类中的“get method”处出错。它说:此行有多个标记(1)此方法必须返回thype Object的结果。(2) 实现AbstractListNode.getIt看起来if和else语句都应该有返回语句。谢谢你的回答和建议。