【问题标题】:Turning a String into a LinkedList and browsing it with recursion将字符串转换为 LinkedList 并使用递归浏览它
【发布时间】:2016-03-08 21:53:26
【问题描述】:

我对递归非常陌生(而且我必须使用它)并且在使用我的一种搜索方法时遇到了一些严重的逻辑问题。请看下面:

//these are methods within a Linked List ADT with StringBuilder functionality
//the goal here is to access the char (the Node data) at a certain index
public char charAt(int index)
{
    if((firstNode == null) || (index < 0) || (index >= length + 1))
    //firstNode is the 1st Node in the Linked List, where the search begins
    {
        System.out.println("Invalid Index or FirstNode is null");
        IndexOutOfBoundsException e = new IndexOutOfBoundsException();
        throw e;
    }
    else
    {
        char c = searchForChar(firstNode, index);
        return c;
    }
}
private char searchForChar(Node nodeOne, int index)
{
    int i = 0;
    if(nodeOne == null) //basecase --> end
    {
        i = 0;
        System.out.println("nodeOne null, returning null Node data");
        return 'n';
    }
    else if(i == index) //basecase --> found
    {
        i = 0;
        return nodeOne.data; //nodeOne.data holds the char in the Node
    }
    else if(nodeOne != null) //search continues
    {
        searchForChar(nodeOne.next, index);
        i++;
        return nodeOne.data;
    }
    return nodeOne.data;
}

输出是“nodeOne null,返回 null 节点数据”的长度为 1 的打印。我不明白最后一个 else-if 语句中的递归语句是如何达到的,而第一个 if 语句中的 null 语句似乎也被达到了。

我尝试重新排列 if 语句,使 if(nodeOne != null) 排在第一位,但这给了我一个 NullPointerException。不知道我做错了什么。特别是因为我可以使用toString() 方法打印节点中的数据,所以我知道节点没有空数据。

谁能帮我理解一下?

【问题讨论】:

  • 如果您发布了一个完整的示例,我想您将有更好的机会在这里获得有意义的答案/建议 - 例如某人可以运行的程序。我建议您添加一个示例,说明如何调用这些方法以及结果与您的预期有何不同。
  • firstNode从何而来?
  • 我做了一些编辑,解释了类、方法和变量的一些用途 - 我希望这会有所帮助。
  • 我认为你不应该使用i 并使用return rearchForChar(nodeOne.next, index - 1); 并检查index == 0,因为这样你就知道你在正确的节点。
  • @martijnn2008 但不应该索引保持不变,因为这两种方法的重点是在该特定索引处找到一个字符?

标签: java if-statement recursion linked-list nodes


【解决方案1】:

我写了一个完整的例子我希望这是你需要的。如果你用i &lt; 14循环字符串StackOverflow,它也会打印空字符\0,如果你使用i &lt; 15,它会给你一个IndexOutOfBoundsException。每次您实际上说我需要 (index - 1) 跳到我的目标节点时,通过将索引减少 1。

public class CharTest {
    public static class Node {
        private char content;
        private Node nextNode;

        public Node () {
            content = '\0';
            nextNode = null;
        }

        public Node (String str) {
            Node temp = this;
            for (int i = 0; i < str.length(); i++) {
                temp.content = str.charAt(i);
                temp.nextNode = new Node();
                temp = temp.nextNode;
            }
        }

        public char charAt(int index) {
            if (index == 0) {
                return content;
            } else if (index < 0 || nextNode == null) {
                throw new IndexOutOfBoundsException();
            }
            return nextNode.charAt(index - 1);
        }
    }
    public static void main(String[] args) {
        Node test = new Node("StackOverflow");
        for (int i = 0; i < 13; i++) { 
            System.out.print(test.charAt(i));
        }
        System.out.println();
    } 
}

我将迭代或递归地创建toString() 方法作为练习留给读者。但是出于性能原因,使用StringBuilderchar[] 将是一个好主意。

【讨论】:

  • 非常感谢!你不必写出所有的代码,但我还是很感激。我不会接受你的代码 - 我只是听到了解我在做什么,我的错误,以及如何成为一个更好的程序员。我也只是在今天早上才意识到我在 charAt() 输出的主要打印长度为 1 中有一个循环。再次感谢!
  • 没问题,请随意接受答案并以任何方式使用代码:)。祝你好运。
  • 我将您的 charAt 方法中的代码改编为我的类和方法,效果非常好!非常感谢您将我推向正确的方向!
猜你喜欢
  • 2010-09-12
  • 2020-02-17
  • 2014-08-10
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 2014-04-25
  • 1970-01-01
相关资源
最近更新 更多