简体   繁体   English

在java中使用Recursive方法

[英]Using Recursive method in java

for homework I was asked to write a contain method for a custom linked list. 对于家庭作业,我被要求为自定义链表编写一个包含方法。 I know that the recursive method should have a base case and then the recursive case.However, I am having some trouble understanding how to write the recursive case of the method. 我知道递归方法应该有一个基本情况,然后是递归情况。但是,我在理解如何编写方法的递归情况时遇到一些麻烦。 So far this is what I have written, but my code is executing the base case more than once. 到目前为止,这是我写的,但我的代码不止一次地执行基本情况。 Can you please give me some guidance? 你能给我一些指导吗?

public class OrderedList {

private Node first;

//Constructor
public OrderedList() {
    this.first = null;
}

//Return the number of items in the list
public int size() {
    int counter = 0;
    Node pointer = this.first;
    while (pointer != null) {
        counter++;
        pointer = pointer.next;
    }
    return counter;
}

//Return an array of copies of the stored elements
public Comparable[] getStore() {

    Comparable[] elements = new Comparable[size()];
    Node pointer = this.first;
    if (this.first == null) {
        return elements;
    } else {
        int i = 0;
        while (pointer != null) {
            elements[i] = pointer.data;
            pointer = pointer.next;
            i++;
        }
        return elements;
    }

}
//true iff item matches a stored element
//Recursive

public boolean contains(Comparable item) {

    //Base case
    if (this.first == null) {

        return false;
    }
    Node pointer = this.first;
    this.first = this.first.next;

    if (pointer.data.compareTo(item) == 0) {

        return true;

    } 
    //Recursive case

    else {

        boolean info = contains(item);
        pointer.next = this.first;
        this.first = pointer;

        return info;
    }
}

First of all I like to do something like this: 首先,我喜欢这样做:

public boolean contains(Comparable item)
{
     return containsHelper(this.first, Comparable item);
}

private boolean containsHelper(Node node, Comparable item)
{
    //base case
    if(node == null)
    {   
         return false;
    }
    else
    {
         if(node.data.compareTo(item) == 0)
         {
             return true;
         }

         return containsHelper(node.next, item);
    }


}

This hides implementation details from the user and it stops your list from getting overridden when you run that method. 这会隐藏用户的实现细节,并在运行该方法时阻止列表被覆盖。

To implement a recursive solution, you need an auxiliary method for contains . 要实现一个递归解决方案,你需要一个辅助方法contains The auxiliary method should have an additional argument that is the Node from which to start testing. 辅助方法应该有一个额外的参数,即从中开始测试的Node The public contains method should call the auxiliary method and pass this.first as the start node. public contains方法应调用辅助方法并将this.first作为起始节点传递。 The rest of the logic should then be pretty simple for you to figure out. 其余的逻辑应该非常简单,你可以弄明白。

From what I am seeing, your code will return true once the else statemnet have been executed once. 从我所看到的,一旦执行了else statemnet,你的代码将返回true。 I think what you need to do is to set the boolean value to false everytime because recursion acts very much like a while loop and if the values are not updated, the base case would be executed over and over again. 我认为你需要做的是每次都将boolean值设置为false,因为递归的行为非常类似于while循环,如果值没有更新,则基本情况会一遍又一遍地执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM