【问题标题】:Recursive toString Method递归 toString 方法
【发布时间】:2013-06-02 20:58:15
【问题描述】:

我正在处理我当前的任务,即创建一个 LinkedList 数据结构,我已经创建了它以及其他方法,它工作得非常好。我的最后一个问题是创建一个 toString 方法。应该是:

"toString 方法返回列表的字符串表示形式。用逗号分隔每个项目,并将项目括在大括号中,例如 {1,4,7,5}。公共 toString 方法必须调用私有的递归方法方法来生成逗号分隔的项目列表。(您可以在公共方法中添加大括号。)"

我的公共 toString 方法正在工作;

    public String toString() {
    int size = getSize();
    String str = "{ ";
    Link current = first;

    for(int i = 0; i < getSize(); i++, current = current.next) {
        str += current.getiData() + " ";
    }

    str += " }";
    return str;
}

(我知道我应该使用 StringBuilder,只是暂时使用 +=。)但是对于私有方法,我什至对编写它感到困惑。现在我能想到的唯一方法是:

private String toString(int x) {
    if(i > 0) {
        toString(--x);
    }
    return ", ";
}

这只是愚蠢的(实际上不是递归),任何人都可以澄清要做什么和/或提供伪代码吗?

【问题讨论】:

    标签: java recursion


    【解决方案1】:

    我认为从代码的角度来看其他答案是合适的,但我只是想添加更多理论来帮助您了解如何到达那里。在进行递归时,您总是需要两件事:基本情况和递归情况。基本情况是非常容易解决的事情,而递归情况是您如何逐步解决可解决的简单情况。

    链表本身就是一种递归数据结构。例如,如果您有一个包含 10 个项目的链接列表,您可以将其想象为一个带有 9 个项目的链接列表的单个节点。

    因此,对于基本情况(再次,借用@Chris 的答案),最简单的可能执行toString() 的列表是一个空列表。所以你的基本情况看起来像这样(伪代码):

    if(list is empty)
    {
       return "";
    }
    

    然后,您的递归案例需要采用您现有的链接列表并尝试向下处理您的基本案例。最简单的方法是打破你知道可以解决的一小部分问题,解决它,然后处理剩下的稍微小一点的问题。在打印链接列表的情况下,这意味着您可以从列表中获取一个项目,将其转换为字符串,然后再处理列表的其余部分。所以你的递归案例看起来像这样(伪代码):

    if(list is not empty)
    {
       String x = take the current node and translate it to a string;
    
       Add x to your running value of the String value of the entire list
    
       Recursively call this function with the next node in the list
       (you reduce your list size by 1 with each call and work down to your base case of an empty list)
    }
    

    希望这可以帮助您了解如何以递归方式解决此问题。如何使其工作肯定有很多变化。没有一种“正确”的递归方式,就像没有一种“正确”的方式来编写循环一样,但一般的“基本案例”和“递归案例”思维模型通常是最好的起点。

    【讨论】:

      【解决方案2】:

      你想要这样的东西(给出递归的基本概念,行不通)

      privat String toString(Link link) {
        if (link.isLast()) {
          return link.value();
        }
        else {
          return link.value() + toString(link.next());
        }
      }
      

      【讨论】:

      • 您可能需要调整该基本情况。这会将 NPE 放在一个空列表中。
      • 我的代码有很多东西不适用。它的目的是作为递归如何工作的指针,而不是一个可行的解决方案。我更喜欢这种方式,因为这显然是家庭作业,因为递归是 OP 的问题。
      【解决方案3】:

      这有点奇怪,因为toString方法的签名没有参数,这意味着需要另一个方法。在链表中,每个节点都有data 和指向下一个节点的pointer

      public String getData(Node n, String value)
      {
           if(n == null)
           {
               // We know we're at the end, so don't proceed.
               return value;
           }
           else
           {
               // n isn't empty, and ignoring the lack of stringbuilder
               value += n.getData();
               // Make a recursive call with the next value in the list, and the new string.
               return getData(n.next(), value);
           }
      }
      

      【讨论】:

      • 或者,您也可以使用return "";return n.value + getData(n.next()); 来避免在整个递归调用链中传递String
      猜你喜欢
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2013-10-12
      • 2018-04-16
      相关资源
      最近更新 更多