索引超出绑定异常错误

时间:2016-11-10 00:40:33

标签: java exception indexing

我一直在努力尝试修复此错误几个小时,我无法弄清楚导致错误的位置/原因  (java.lang.IndexOutOfBoundsException:index:68 size:26)

这会在所有大写字母中创建字母

ps

我怀疑问题的原因是这两个循环中的一个

将数组字母添加到链表并将其设置为节点

String [] myStringsChars= new String[26];
        for(int i = 0; i < 26; i++)
        {
            myStringsChars[i] = new String(Character.toChars(i+65));
            System.out.println(myStringsChars[i]);

        }

设置节点父节点和子节点

int j=0;
    while (j<myStringsChars.length){

        BinaryTree.add(alphabet = new TreeNode(myStringsChars[j]));
        if (j<=26){
            j++;
        }
    }

这是我的其余代码以防万一

    int k =0;

    while (k<BinaryTree.size()){
        int find=(k-1)/2;
        BinaryTree.get(k).setParent(BinaryTree.get(find));

        if(k%2 ==0){
            (BinaryTree.get(k).getParent()). setRightChild(BinaryTree.get(k));
        }
        else{
            (BinaryTree.get(k).getParent()).setLeftChild(BinaryTree.get(k));
        }
        k++;
    }

TreeNode节点

import java.util.*;

public class TreeExercise
{

    public static void main(String args[])
    {


        String [] myStringsChars= new String[26];
        for(int i = 0; i < 26; i++)
        {
            myStringsChars[i] = new String(Character.toChars(i+65));
            System.out.println(myStringsChars[i]);

        }
        List<TreeNode> BinaryTree = new LinkedList();

        int j=0;
        while (j<myStringsChars.length){

            BinaryTree.add(alphabet = new TreeNode(myStringsChars[j]));
            if (j<=26){
                j++;
            }
        }
        int k =0;

        while (k<BinaryTree.size()){
            int find=(k-1)/2;
            BinaryTree.get(k).setParent(BinaryTree.get(find));

            if(k%2 ==0){
                (BinaryTree.get(k).getParent()). setRightChild(BinaryTree.get(k));
            }
            else{
                (BinaryTree.get(k).getParent()).setLeftChild(BinaryTree.get(k));
            }
            k++;
        }
        BinaryTree.get(0).setParent(null);



        Scanner input= new Scanner(System.in);
        String userChoice="";
        while (!(userChoice.equals("end"))){
            System.out.println("enter two CAPITAL letters to find their common ancestor ex.(DC)\n type 'end' to end program");
            userChoice= input.nextLine();
            char letter1=userChoice.charAt(0);
            char letter2=userChoice.charAt(1);
            int let1= (int)letter1;
            int let2= (int)letter2;
            if(userChoice.length()<=2){
                // cant find BinaryTree ERROR

                TreeNode commonAncestor= findLowestCommonAncestor(root, BinaryTree.get(let1), BinaryTree.get(let2));
                if (commonAncestor !=null){
                    System.out.println(commonAncestor.getContents());
                    }
                System.out.println("Result is: " + "D");
            }
            else if (userChoice.equals("end")){
                System.exit(0);
            }
            else{
                System.out.println("you must type in 2 capital letters");
                userChoice=input.nextLine();
            }
        }
    }   

    public static TreeNode findLowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2)
    {
findLowestCommonAncestor(root.getRightChild(), node1, node2)
        //every time
        TreeNode rightChild= findLowestCommonAncestor(root.getRightChild(), node1, node2);
        TreeNode leftChild= findLowestCommonAncestor(root.getLeftChild(), node1, node2);
        if (leftChild != null && rightChild!=null){
            return root;
        }
        if(root==null){
            return null;
        }


        if (leftChild!=null){
            return leftChild;
        }
        if(root.getContents()==node1 || root.getContents()==node2){
            return root;
        }

        else {
            return rightChild;
        }

    }       
}

1 个答案:

答案 0 :(得分:1)

您的错误似乎在这里,猜测这是行<body> <header> <div class="titleborder"><span class="title">Dank nasty title</span> <a href="https://facebook.com" target="_blank">This is a link</a></div> </header> </body>

TreeExercise.java:113

您的树状列表的索引编号为0到25,但int let1= (int)letter1; int let2= (int)letter2; if(userChoice.length()<=2){ // cant find BinaryTree ERROR TreeNode commonAncestor= findLowestCommonAncestor(root, BinaryTree.get(let1), BinaryTree.get(let2)); ^^^^ ^^^^ let1let2输入为DE68。所以,试试:

69

使用int let1= (int)letter1 - 'A'; int let2= (int)letter2 - 'A'; 而不是'A'时,您的其他代码也会更清晰。