arrayOutOfBounds异常java

时间:2013-04-03 11:40:05

标签: java arrays

我想解析一个文本文件,并根据我在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是我通过在解析方法中传入单词而创建的节点

这是代码:

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);
 }

}

有人会帮忙吗? 该文件包含的所有内容:

“允许其用户通过网络浏览器添加修改或删除其内容的网站”

Node类:

   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;
    }

}

4 个答案:

答案 0 :(得分:0)

检查此行(27):

Node addChar1 = new Node(characters[0]);

传入的String是否包含任何字符?检查输入文件中是否有空行。

答案 1 :(得分:0)

您应该在访问之前检查索引0和1:

if (!s.isEmpty()) {
       char[] characters = s.toCharArray();
       Node addChar1 = new Node(characters[0]);
       Node addChar2 = new Node(characters[1]);
...

你显然有一些空字符串。

答案 2 :(得分:0)

使用此代码时

Node addChar1 = new Node(characters[0]);
Node addChar2 = new Node(characters[1]);

在访问索引元素之前,您应该检查字符数组的大小。当字符数组大小小于2时,您将获得ArrayIndexOutOfBoundsException

答案 3 :(得分:0)

char [] characters = s.toCharArray(); //检查字符数组长度然后写字符[1]

相关问题