作者:若涵她娘_124 | 来源:互联网 | 2023-08-01 10:08
I've been working on trying to fix this error for a few hours and i cant figure out where/what is causing the error (java.lang.IndexOutOfBoundsException: index:68 size: 26)
我一直在努力尝试修复此错误几个小时,我无法弄清楚导致错误的位置/原因(java.lang.IndexOutOfBoundsException:index:68 size:26)
this creates the alphabet in all caps
这会创建所有大写字母
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]);
}
i suspect the cause of the problem is one of these two loops
我怀疑问题的原因是这两个循环中的一个
adds array letters to a linked list and sets it as a node
将数组字母添加到链接列表并将其设置为节点
int j=0;
while (j
sets the node parents and children
设置节点父节点和子节点
int k =0;
while (k
here's the rest of my code in case it helps
这是我的其余代码,以防它有所帮助
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 BinaryTree = new LinkedList();
int j=0;
while (j
TreeNode nodes
TreeNode节点
public class TreeNode{
private T contents;
private TreeNode parent;
private TreeNode leftChild;
private TreeNode 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 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 个解决方案