【问题标题】:Replace collections in Tree implementatnion替换树实现中的集合
【发布时间】:2017-03-17 18:34:36
【问题描述】:

我有一个 Tree 实现,但我想将 ArrayList 更改为简单的数组,我不想在 java 中使用集合我只想使用数组,但我不知道如何将 ArrayList 替换为简单的数组。 有一个代码:

public class TreeNode {
    private String data = null;
    private List<TreeNode> children = new ArrayList<>();
    int topSize;// I added it, since i know how is the size of Tree
    private TreeNode[] children2 = new TreeNode[topSize];//I added it
    private TreeNode parent = null;

    public TreeNode(String data) {
        this.data = data;
    }

    int i = 0;//I added it

    public void addChild(TreeNode child) {
        child.setParent(this);
        this.children.add(child);
        this.children2[i++] = child;//I added it
    }

    public void addChild(String data) {
        TreeNode newChild = new TreeNode(data);
        newChild.setParent(this);
        children.add(newChild);
        children2[i] = newChild;// I added it
    }

    public void addChildren(List<TreeNode> children) {
        for (TreeNode t : children) {
            t.setParent(this);
        }
        this.children.addAll(children);

    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    private void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public TreeNode getParent() {
        return parent;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode("Root");

        TreeNode child1 = new TreeNode("Child1");
        child1.addChild("Grandchild1");
        child1.addChild("Grandchild2");

        TreeNode child2 = new TreeNode("Child2");
        child2.addChild("Grandchild3");

        root.addChild(child1);
        root.addChild(child2);
        root.addChild("Child3");

        root.addChildren(Arrays.asList(
                new TreeNode("Child4"),
                new TreeNode("Child5"),
                new TreeNode("Child6")
        ));
        TreeNode mainRoot = new TreeNode("MainRoot");
        mainRoot.addChildren(Arrays.asList(root));

        for (TreeNode node : root.getChildren()) {
            System.out.println(node.getData());
        }
    }
}

我创建 TreeNode[] children2 数组并添加 int topSize,因为我知道树的大小。但它不能正常工作。我想要没有来自 java.java.lang.ArrayIndexOutOfBoundsException 集合的 Tree: 0 in children2[i] = newChild;

【问题讨论】:

  • 你面临什么问题? “它不能正常工作”有点太含糊了。
  • 我有 :java.lang.ArrayIndexOutOfBoundsException: 0 in children2[i] = newChild;

标签: java arrays tree


【解决方案1】:

topSize 变量从未在我们的代码中赋值,因此它具有默认值:0。这意味着children2 是一个空数组。这就是为什么您在向其中添加内容时会收到 ArrayIndexOutOfBoundsException

尝试用一些值初始化topSize,例如:

int topSize = 10;

修改后,你的程序运行无异常。

【讨论】:

  • 谢谢,现在可以了,你能告诉我为什么当我尝试这样设置 topSize 时它不起作用:` public Tree(String data, int topSize) { this.data = data; this.topSize = topSize; }` 主要是int topSize = 10; Tree root = new Tree("Root",topSize);
  • 如果对您有帮助,请接受我的回答。至于第二个问题,试试Tree root = new Tree("Root",10);
  • 我尝试过,但它不起作用我有:~int topSize ; Tree[] children2 = new Tree[topSize]; private Tree parent = null; public Tree(String data, int topSize) { this.data = data; this.topSize = topSize; }~ 主要是:Tree root = new Tree("Root",10); Tree child1 = new Tree("Child1",10); child1.addChild("Grandchild1"); child1.addChild("Grandchild2"); Tree child2 = new Tree("Child2",10); child2.addChild("Grandchild3"); root.addChild(child1); root.addChild(child2); root.addChild("Child3");
  • 我有错误:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 in children2[i++] = newChild;
  • @grapexs,我建议你使用一些调试技术。任何体面的 Java IDE(IntelliJ IDEA、Eclipse、Netbeans)都有一个内置的调试器,使用它可以很容易地逐步跟踪您的程序并查看其执行过程中究竟发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2015-01-17
相关资源
最近更新 更多