热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

索引超出绑定异常错误-IndexOutOfBoundExceptionError

Ivebeenworkingontryingtofixthiserrorforafewhoursandicantfigureoutwherewhatisca

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 个解决方案

#1


1  

Your error appears to be here, guessing that this is line TreeExercise.java:113:

您的错误似乎在这里,猜测这是行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));
                                       ^^^^                  ^^^^

Your tree list is indexed from 0 to 25, but let1 and let2, given an input of DE, are 68 and 69. So, try:

您的树列表的索引编号为0到25,但给定DE输入的let1和let2为68和69.所以,请尝试:

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

It would be clearer in your other code, too, to use 'A' rather than 65.

你的其他代码也会更清楚地使用'A'而不是65。


推荐阅读
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 学习Java异常处理之throws之抛出并捕获异常(9)
    任务描述本关任务:在main方法之外创建任意一个方法接收给定的两个字符串,把第二个字符串的长度减1生成一个整数值,输出第一个字符串长度是 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 本文探讨了C语言中指针的应用与价值,指针在C语言中具有灵活性和可变性,通过指针可以操作系统内存和控制外部I/O端口。文章介绍了指针变量和指针的指向变量的含义和用法,以及判断变量数据类型和指向变量或成员变量的类型的方法。还讨论了指针访问数组元素和下标法数组元素的等价关系,以及指针作为函数参数可以改变主调函数变量的值的特点。此外,文章还提到了指针在动态存储分配、链表创建和相关操作中的应用,以及类成员指针与外部变量的区分方法。通过本文的阐述,读者可以更好地理解和应用C语言中的指针。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
author-avatar
若涵她娘_124
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有