运行以下代码时出现错误。我已提供评论来描述正在发生的事情。
// Node
class TreeNode {
char value;
TreeNode left;
TreeNode right;
}
// Main class
public class BinaryTree {
// Global variables
char[] preorder;
int i = 0;
// Main method runs gatherOutput
public static void main(String[] args) throws IOException {
new BinaryTree().gatherOutput();
}
// This takes a null tree as input from the gatherOutput method
// and whenever a 0 is encountered in the preorder character array
// (from a string from System.in) a new external node is created with
// a value of 0. Whenever a letter is encountered in the character
// array, a new internal node is created with that letter as the value.
//
// =====ArrayOutOfBoundsException occurs somewhere here=====
//
public TreeNode createTree(TreeNode tree) throws IOException {
if (preorder[i] == 0) {
tree = new TreeNode();
tree.value = 0;
tree.left = tree.right = null;
i++;
} else {
tree = new TreeNode();
tree.value = preorder[i];
i++;
createTree(tree.left);
createTree(tree.right);
}
return tree;
}
// Supposed to print out contents of the created binary trees.
// Just for testing purposes, but it's not working right now for some reason.
public void preorderTraversal(TreeNode tree) {
if (tree != null) {
System.out.println(tree.value + " ");
preorderTraversal(tree.left);
preorderTraversal(tree.right);
}
}
// Reads System.in for the Strings used in making the binary tree
// and is supposed to make a different binary tree for every line of input
public void gatherOutput() throws IOException {
TreeNode tree = null;
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
preorder = reader.readLine().toCharArray();
while (reader.readLine() != null) {
tree = createTree(tree);
preorderTraversal(tree);
i = 0;
}
}
}
每当我有多于一行的输入时,我都会收到 ArrayIndexOutOfBounds 错误。例如:
b
b
堆栈跟踪:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at btsmall.createTree(btsmall.java:22)
at btsmall.createTree(btsmall.java:31)
at btsmall.gatherOutput(btsmall.java:53)
at btsmall.main(btsmall.java:18)
它发生在 createTree 方法中,但我无法查明原因。即使我只有一行输入, preorderTraversal 方法似乎也没有运行,因为运行程序没有得到任何输出,但我不知道为什么。有人可以帮我吗?
谢谢。
编辑:我对以下两个方法进行了更改,并且不再收到 ArrayIndexOutOfBounds 错误。
public void createTree(TreeNode tree) throws IOException {
if (i >= preorder.length) {
i++;
} else if (preorder[i] == '0') {
tree = new TreeNode();
tree.value = '0';
tree.left = tree.right = null;
i++;
} else {
tree = new TreeNode();
tree.value = preorder[i];
i++;
createTree(tree.left);
createTree(tree.right);
}
}
public void gatherOutput() throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String line = null;
TreeNode tree = new TreeNode();
while ((line = reader.readLine()) != null) {
preorder = line.toCharArray();
tree = createTree(tree);
preorderTraversal(tree);
i = 0;
}
}
但是,preorderTraversal 的输出打印出单个方 block ,而不是预序中所有节点的值。
最佳答案
这里发生的事情很少。最突出的是您正在读取一行,然后尝试立即读取另一行,如下所示。
preorder = reader.readLine().toCharArray();
while (reader.readLine() != null) {
您只需阅读该行一次。例如:
String line = null;
while ((line=reader.readLine()) != null){
System.out.println(line);
preorder = line.toCharArray();
}
另外,您的支票:
if (preorder[i] == 0) {
当你到达行尾时,将会爆炸,因为你从不检查i
是否超出了数组的范围。我不确定您真正想要在那里检查什么。
关于java - 是什么导致此代码中的 ArrayIndexOutOfBounds 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5898218/