2016-11-10 1 views
0

私はこのエラーを2,3時間修正しようとしていましたが、エラーの原因/原因を特定できませんでした。 (インデックス:68サイズ:26 )インデックスがバインドされた例外エラーのエラー

これは私が問題の原因は、これら2つのループ

の一つである疑いがすべて大文字でアルファベット

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

は、それが

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

    }  
} 

のTreeNodeノード

public class TreeNode<T extends Comparable>{ 
    private T contents; 
    private TreeNode<T> parent; 
    private TreeNode<T> leftChild; 
    private TreeNode<T> rightChild; 
    private int level; 

    public TreeNode() 
    { 
     //added 
     //parent=null; 
     //leftChild=null; 
     //rightChild=null; 
     //level=0; 
    } 
    public TreeNode(T data){ 
    contents=data; 
    this.parent=parent; 
} 

    public TreeNode(T data, TreeNode parent) 
    { 
     contents = data; 
     this.parent = parent; 
    }   

    public void setLeftChild(TreeNode node) 
    { 
     this.leftChild = node; 
    }   

    public void setRightChild(TreeNode node) 
    { 
     this.rightChild = node; 
    }   

    public boolean isContentEquals(T data) 
    { 
     return 0 == getContents().compareTo(data); 
    } 

    /** 
    * @return the contents 
    */ 
    public T getContents() { 
     return contents; 
    } 

    /** 
    * @param contents the contents to set 
    */ 
    public void setContents(T contents) { 
     this.contents = contents; 
    } 

    /** 
    * @return the parent 
    */ 
    public TreeNode getParent() { 
     return parent; 
    } 

    /** 
    * @param parent the parent to set 
    */ 
    public void setParent(TreeNode parent) { 
     this.parent = parent; 
    } 

    /** 
    * @return the leftChild 
    */ 
    public TreeNode getLeftChild() { 
     return leftChild; 
    } 

    /** 
    * @return the rightChild 
    */ 
    public TreeNode getRightChild() { 
     return rightChild; 
    } 
    /** 
    * Given an object T contentToSearch, this method returns 
    * the node that stores the contentToShare or null if not found on the current tree 
    * @return the node 
    */ 
    public TreeNode findNodeOnTree(T contentToSearch) 
    { 
     List<TreeNode> nodes = new LinkedList(); 
     nodes.clear(); 
     nodes.add(this); 

     while(!nodes.isEmpty()) 
     { 
      TreeNode current = nodes.remove(0); 
      if(current.isContentEquals(contentToSearch)) 
      { 
       return current; 
      } 

      if(current.leftChild != null) 
      { 
       nodes.add(current.leftChild); 
      } 

      if(current.rightChild != null) 
      { 
       nodes.add(current.rightChild); 
      }  
     } 

     return null; 
    }   

    /** 
    * @return the level 
    */ 
    public int getLevel() { 
     return level; 
    } 

    /** 
    * @param level the level to set 
    */ 
    public void setLevel(int level) { 
     this.level = level; 
    } 

} 
+1

例外の完全なスタックトレースを送信して、スタックトレースで報告されたコード行を指定してください。 –

+1

原因を絞り込むには、デバッグする必要があります。 – Carcigenicate

+0

java.lang.IndexOutOfBoundsException:インデックス:68、サイズ:26 java.util.LinkedList.checkElementIndexで\t(LinkedList.java:555)java.util.LinkedList.getで \t(LinkedList.java:476) \t TreeExercise.main(TreeExercise.java:113) ここに完全なエラーメッセージがあります。 TreeNode commonAncestor = findLowestCommonAncestor(root、BinaryTree.get(let1)、BinaryTree.get(let2));それはエラーラインだと言いますが、それは根本的な原因のようには見えません – baconbacon

答えて

1

に役立つ場合には、ここでノードの親と子

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

はあなたの誤差があるように思われる私のコードの残りの部分だ設定しますここでは、これが線であると推測しますTreeExercise.java:113

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

あなたのツリーリストが0から25までインデックスを付け、しかしlet1let2DEの入力を与え、6869あるれます。だから、試してみてください。

int let1= (int)letter1 - 'A'; 
int let2= (int)letter2 - 'A'; 

それはあまりにも、あなたの他のコードで明確になりますが、'A'いうより65使用します。

+0

それはエラーを修正しましたが、もちろん新しいものがポップしますfindLowestCommonAncestorメソッドを実行しようとしたときに起動します。 "TreeNode rightChild = findLowestCommonAncestor(root.getRightChild()、node1、node2);" – baconbacon

+0

'root == null 'のチェックを' findLowestCommonAncestor() 'の先頭に移動します。あなた自身でデバッグを続けてください。つまった場合は、新しい質問を投稿してください。 –

+1

ケンの答えを受け入れることはクールなジェスチャーになるだろう...そして彼はポイントを得る! – runningviolent

関連する問題