gpt4 book ai didi

Java 用递归填充 3D 链表

转载 作者:行者123 更新时间:2023-11-29 04:53:31 30 4
gpt4 key购买 nike

我在尝试填充 3D 链表时出现堆栈溢出。我不明白为什么它没有在指定的范围内停止,它只是永远运行。这可能是一个简单的错误,我只是不明白。

编辑:好的,我现在已经更新了代码并删除了那个愚蠢的错误,但是它仍然没有完全按预期运行。它似乎正在生成 10x10x10 列表,但它会无限运行。用 (10, 10, 10) 初始化,它应该创建 10000 个对象并停止。我只是想创建一个列表来表示 3d 坐标平面,每个整数坐标是一个节点,可通过方向指针北、南、东、西、上或下访问。

感谢任何帮助

public class Main {


public static void main(String[] args) {
NodeController3D con = new NodeController3D(6,6, 6);

}

}
public class Node3D {

// public Node3D(Node3D... nodes) {
// if (nodes.length != 5) {
// throw new RuntimeException();
// }
// this.nodes = nodes;
// }

public Node3D[] nodes;
public int x, y, z;


public Node3D north() {
return nodes[0];
}

public Node3D south() {
return nodes[1];
}

public Node3D east() {
return nodes[2];
}

public Node3D west() {
return nodes[3];
}

public Node3D up() {
return nodes[4];
}

public Node3D down() {
return nodes[5];
}
}

public class NodeController3D {

public NodeController3D(int length, int width, int height) {
HEAD = new Node3D();
pnc(HEAD, length, width, height);
}

private void pnc(Node3D node, int xMax, int yMax, int zMax) {
if(node.nodes == null) {
node.nodes = new Node3D[5];
}

if (node.x < xMax) {
Node3D newNode = node.nodes[2] = new Node3D();
newNode.x = node.x + 1;
newNode.y = node.y;
newNode.z = node.z;
System.out.println(newNode.x + ", " + newNode.y + ", " + newNode.z);

pnc(newNode, xMax, yMax, zMax);
}
if (node.y < yMax) {
Node3D newNode = node.nodes[0] = new Node3D();
newNode.x = node.x;
newNode.y = node.y + 1;
newNode.z = node.z;

pnc(newNode, xMax, yMax, zMax);
}
if (node.z < zMax) {
Node3D newNode = node.nodes[4] = new Node3D();
newNode.x = node.x;
newNode.y = node.y;
newNode.z = node.z + 1;

pnc(newNode, xMax, yMax, zMax);
}
}

// public NodeController3D(int radius) {
//
// }

public final Node3D HEAD;
}

编辑:好的,我现在已经更新了代码并删除了那个愚蠢的错误,但是它仍然没有完全按预期运行。它似乎正在生成 10x10x10 列表,但它会无限运行。用 (10, 10, 10) 初始化,它应该创建 10000 个对象并停止。我只是想创建一个列表来表示 3d 坐标平面,每个整数坐标都是一个节点,可通过方向指针访问。

最佳答案

您正在遇到无限递归。

所以发生了什么。

您正在创建一个新的 Array .

if(node.nodes == null) {
node.nodes = new Node3D[5];
}

您继续使用 Node3D作为newNode多变的。发生这种情况是因为 node.x<xMax将是真实的。 -> Node3D newNode = node.nodes[2] = new Node3D(); .

你递归调用pnc现在,有了这个 newNode .

那么现在发生了什么,node.y<yMax会是真的。现在您重新分配新节点。 Node3D newNode = node.nodes[0] = new Node3D(); .并调用pnc再次递归。但是你现在遇到了问题。因为它是新的 Node3D你的node.x<xMax将再次为真,这两个步骤将再次发生并且无限发生,直到您遇到上述错误。

要修复此错误,您可能需要复制 node.xnode.y到您新创建的变量中。

通过改变赋值你可以跳出无限递归。

if (node.x < xMax) {
if (node.nodes[2] == null) {
node.nodes[2] = new Node3D();
}
Node3D newNode = node.nodes[2];
newNode.x = node.x + 1;
newNode.y = node.y;
newNode.z = node.z;

pnc(newNode, xMax, yMax, zMax);
}
if (node.y < yMax) {
if (node.nodes[0] == null) {
node.nodes[0] = new Node3D();
}
Node3D newNode = node.nodes[0];
newNode.x = node.x;
newNode.y = node.y + 1;
newNode.z = node.z;

pnc(newNode, xMax, yMax, zMax);
}
if (node.z < zMax) {
if (node.nodes[4] == null) {
node.nodes[4] = new Node3D();
}
Node3D newNode = node.nodes[4];
newNode.x = node.x;
newNode.y = node.y;
newNode.z = node.z + 1;

pnc(newNode, xMax, yMax, zMax);
}

但由于我不知道您试图通过数组中这些特定索引处的这些特定元素实现什么,这对您来说可能是一个错误的解决方案。

关于Java 用递归填充 3D 链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588804/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com