【问题标题】:Stack Overflow error: How would I write this method Iteratively?堆栈溢出错误:我将如何迭代地编写此方法?
【发布时间】:2014-06-04 21:55:34
【问题描述】:

我在递归调用中收到以下方法的堆栈溢出错误。我想我需要让它迭代才能让它工作。我将如何迭代编写以下方法?

Node myMethod(String foo, Node p) {
   if(p == null) {
       p = new Node(foo);
       p.link = spot;
       spot = p;
       p.calc[bar] = 1;
   return p;
   } else if(foo.equals(p.origin)) {
       p.calc[bar] = p.calc[bar] + 1;
       return p;
   } else { 
         while (p.next == null & foo.equals(p.origin)){
             p = p.next;
         }
       p.next = myMethod(foo, p.next);
       return p;
   }
}

p 是一个 Node 类,它具有 String fooString originNode linkNode next 和 int Array calc[]bar 是一个整数。 spot 是一个随机节点。

这是我迄今为止尝试过的

Node myMethod(String foo, Node p) {
if(p == null) {
   p = new Node(foo);
   p.link = spot;
   spot = p;
   p.calc[bar] = 1;
return p;
} else if(foo.equals(p.origin)) {
    p.calc[bar] = p.calc[bar] + 1;
    return p;
} else { 
    while (p.next == null & foo.equals(p.origin)){
         p = p.next;
    }
//instead of doing recursion on: p.next = myMethod(foo, p.next);. I tried the following:
    if (p.next == null){
        p.next = new Node(foo);
        p.next.link = spot;
        spot = p.next;
        p.next.calc[bar] = 1;
    } else if (foo.equals(p.next.origin)){
        p.next.calc[bar] = p.next.calc[bar] + 1;
    } else {
        while (p.next.next == null & foo.equals(p.next.origin)){
        p.next = p.next.next;
        }
      }
   }
 return p;
 }

【问题讨论】:

  • 另外,像myMethod 这样的名字并没有告诉我们这个方法的目的......
  • @OliCharlesworth 查看编辑。 myMethod 创建一个节点(链表的一部分)并返回它。
  • 你不是指while (p.next == null & foo.equals(p.origin)中的&&吗?您似乎不打算按位操作。
  • 我将 & 替换为 &&,但仍然出现 Stack Overflow 错误。
  • while 循环在您的递归算法中没有任何意义:条件永远不会为真。应该是:!= null 和 !等于?

标签: java recursion iteration stack-overflow


【解决方案1】:

虽然您的迭代尝试可能会写得更好,但这是一个很好的尝试。但是,请看这里:

else { 
    while (p.next == null & foo.equals(p.origin)){ //HERE! Check the while argument.
         p = p.next;
    } //instead of doing recursion on: p.next = myMethod(foo, p.next);. I tried the following:
    …

如果 p.next 为 null 或 foo 不等于 p.origin,while 循环将停止。现在的基本方法是在这里服务三种情况:

  1. p.next != null(第一个 while 条件)时该怎么办
  2. !foo.equals(p.origin)(第二个while条件)//你没有写的时候怎么办。
  3. p.next != null && !foo.equals(p.origin) 时该怎么办

如果您的 while 条件是正确的(您希望它们是正确的),您需要处理所有可能会停止 while 循环并且应该结束您的算法的情况。如果不是,请重新考虑您的 while 循环条件。

【讨论】:

    【解决方案2】:

    我仍然不完全确定您要做什么,这段代码看起来像糟糕的编程风格,但也许它会满足您的需求。

    Node myMethod(String foo, Node p) {
        while(p != null) {
            if(foo.equals(p.origin)) {
                p.calc[bar] = p.calc[bar] + 1;
                break;
            }
            p = p.next;
        }
        if(p == null) {
            p = new Node(foo);
            p.link = spot;
            spot = p;
            p.calc[bar] = 1;
        }
        return p;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2014-01-26
      • 2019-02-16
      相关资源
      最近更新 更多