我想根据我在 addToTree 方法中指定的规则解析一个文本文件并从中构建一棵树。但是,我收到此错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ie.gmit.TreeTest.addToTree(TreeTest.java:27)
at ie.gmit.TreeTest.parse(TreeTest.java:20)
at ie.gmit.TreeTest.main(TreeTest.java:77)
addChar1 和 addChar2 是我通过在 parse 方法中传入的单词创建的节点
这是代码:
public class TreeTest {
public void parse(File f) throws Exception {
Node root = new Node('+'); //create a root node
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line;
while((line = br.readLine())!=null){
String[] words = line.toLowerCase().split(" ");
for(int i = 0; i < words.length; i++){
addToTree(words[i], root);
}
}
}
public void addToTree(String s, Node root){
char[] characters = s.toCharArray();
Node addChar1 = new Node(characters[0]);
Node addChar2 = new Node(characters[1]);
Node fullWord = new Node(s);
//get the child nodes of the root
Node[] rootChildren = root.children();
//get the child nodes of the first node (addChar1)
Node[] addChar1Children = addChar1.children();
//get each child of the root
for(int i=0; i<rootChildren.length; i++){
Node rootChild = rootChildren[i];
//see if the addChar1 already exists in the tree
//if it doesn't
if(!rootChild.equals(addChar1)){
//add the addChar1 as a child of the root
root.addChild(addChar1);
//add the addChar2 as a child of the addChar1 also
addChar1.addChild(addChar2);
//insert the whole word as the child of the addChar2
addChar2.addChild(fullWord);
}
//if the addChar1 exists in the tree already
else{
// get each child of the addChar1
for(int j=0; j<addChar1Children.length; j++){
Node addChar1Child = addChar1Children[i];
//see if the addChar2 already exists in the tree
//if it doesn't
if(!addChar1Child.equals(addChar2)){
//add the addChar2 as the child if the addChar1
addChar1.addChild(addChar2);
//add the actual word
addChar2.addChild(fullWord);
}
//if the addChar2 exists the the tree already
else{
//insert the whole word as the child of the FOUND NODE
addChar1Child.addChild(fullWord);
}
}//end of second for loop
}
}//end of the first for loop
}//end of addToTree
public static void main(String[] args) throws Exception {
TreeTest test = new TreeTest();
File f = new File("textFile.txt");
test.parse(f);
}
}
有人会帮忙吗?该文件包含的所有内容:
“通常允许其用户通过网络浏览器添加修改或删除其内容的网站”
节点类:
public class Node<E> {
private Node parent;
private String fullWord;
private char character; // value inside a node
private boolean word; // put a true flag if the node is a word eg 'a'
private List<Node> children = new ArrayList<Node>(); //creates a list of array list objects
//** constructors **/
public Node(){
}
public Node(String fullWord){
this.fullWord = fullWord;
}
public Node(Node parent){
this.parent = parent;
}
public Node(char character){
this.character = character;
}
public Node(boolean word){
this.word = word;
}
public Node(Node parent, char character){
this(parent);
this.character = character;
}
public Node(Node parent, char character, boolean word){
this(parent);
this.character = character;
this.word = word;
}
//** methods **/
public boolean isRoot(){
return this.parent == null;
}
public boolean hasChildren(){
return this.children.size() > 0;
}
public void addChild(Node child){
child.setParent(this);
children.add(child);
}
public Node getParent(){
return this.parent;
}
public void setParent(Node parent){
this.parent = parent;
}
public Node[] children(){
return (Node[]) children.toArray(new Node [children.size()]);
}
public char getItem() {
return character;
}
}