使用TreeNodes的Java Out of bounds异常

时间:2015-12-05 09:58:07

标签: java arrays indexoutofboundsexception treenode

public static void main(String args[])
{
    //creates tree as linked structure
    int lvl = 0;
    LinkedList<TreeNode> lcaTree = new LinkedList<>();
    TreeNode[] charArray = new TreeNode[lcaTree.size()];
    String[] myStringChars = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
                                          "Q","R","S","T","U","V","W","X","Y","Z"};

    for(int i = 0; i < 26; i++)
    {
        myStringChars[i] = new String(Character.toChars(i+65));
        System.out.println(myStringChars[i]);
        //reads removed value from queue as an Object; needs to be a TreeNode
    }

    // create the Tree as a linked structure from the array myStringChars
    // the Strings are stored using the representation for trees as arrays in the book
    // (e.g. for an element i, the left child is stored in position 2*i + 1, right child is 
    // on position 2*(i + 1). Also specify the level of a TreeNode

    for (int i = 0; i < myStringChars.length; i++){
        TreeNode parent = null;
        if (i == 0){
            parent = new TreeNode<>("A", null);
            parent.setLevel(lvl);
            lvl++;
        }
        else{
            //set level
            if((i == 3)||(i == 7)||(i == 15)){
                lvl++;
            }
            else if (myStringChars[i].toCharArray()[0]%2 == 1){
                parent = new TreeNode<>(myStringChars[i], lcaTree.get((i/2)-1));
                lcaTree.get((i/2)-1).setRightChild(parent);
                parent.setLevel(lvl);
            }
       *****else{
                parent = new TreeNode<>(myStringChars[i], lcaTree.get(i/2));
                lcaTree.get((i/2)).setLeftChild(parent);
                parent.setLevel(lvl);*****
            }
            lcaTree.add(parent);
        }
    }
    //creates array from tree
    charArray[0] = lcaTree.get(0);
    for (int i = 0; i < charArray.length-1; i++){
        if (2*(i+1) < charArray.length){
            charArray[2*(i+1)] = lcaTree.get(i).getRightChild();
        }
        if ((2*i)+1 < charArray.length){
            charArray[(2*i)+1] = lcaTree.get(i).getLeftChild();
        }
    }
    // create a traversal by levels and print as you traverse to check that the creation of the tree has happened correctly

    for (int i = 0; i < charArray.length; i++){
        System.out.print(charArray[i].getContents());
        if (i==0||(i==2)||(i==6)||(i==14)){
            System.out.println();
        }
    }

大家好。所以这是我在使用TreeNodes和基本Java内容时所做的一点练习,我遇到了一个错误,我可以用一些新鲜的眼睛来帮助识别。

我在围绕着星号的声明中得到了一个超出范围的例外。我不知道为什么i / 2会导致越界条件,因为0/2是0和26/2返回13(最小/最大i值),两者都不超出范围。

有人能提供进一步的见解吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

i=0次迭代中,您已在if(i==0)块中创建了根节点,但尚未将其添加到lcaTree

在你的i = 1else中的下一次迭代lcaTree.get(0)将抛出OutOfBounds因为你在位置0上什么都没有,你的数组是零长度。

所以,修复你的for循环:

   for (int i = 0; i < myStringChars.length; i++){
        TreeNode parent = null;
        if (i == 0){
            parent = new TreeNode<>("A", null);
            parent.setLevel(lvl);
            lvl++;
        }
        else{
            //set level
            if((i == 3)||(i == 7)||(i == 15)){
                lvl++;
            }
            else if (myStringChars[i].toCharArray()[0]%2 == 1){
                parent = new TreeNode<>(myStringChars[i], lcaTree.get((i/2)-1));
                lcaTree.get((i/2)-1).setRightChild(parent);
                parent.setLevel(lvl);
            }
            else{
                parent = new TreeNode<>(myStringChars[i], lcaTree.get(i/2));
                lcaTree.get((i/2)).setLeftChild(parent);
                parent.setLevel(lvl);
            }
            lcaTree.add(parent); //<--- move out!
        }
    }

并将lcaTree.add(parent);置于其他条件之外?

    for (int i = 0; i < myStringChars.length; i++){
        TreeNode parent = null;
        if (i == 0){
            parent = new TreeNode<>("A", null);
            parent.setLevel(lvl);
            lvl++;
        }
        else{
            //set level
            if((i == 3)||(i == 7)||(i == 15)){
                lvl++;
            }
            else if (myStringChars[i].toCharArray()[0]%2 == 1){
                parent = new TreeNode<>(myStringChars[i], lcaTree.get((i/2)-1));
                lcaTree.get((i/2)-1).setRightChild(parent);
                parent.setLevel(lvl);
            }
            else{
                parent = new TreeNode<>(myStringChars[i], lcaTree.get(i/2));
                lcaTree.get((i/2)).setLeftChild(parent);
                parent.setLevel(lvl);
            }
        }
        lcaTree.add(parent); // <- like so!
    }