当前位置: 首页 > 知识库问答 >
问题:

Java中如何纠正Arraylist索引越界长度错误

荀子轩
2023-03-14

我正在为学校写一个充当收银机的程序。我要求输入项目的价格,并将它们播放到一个正在进行的ArrayList中,直到用户输入-1或0。0是在错误的情况下重新输入以前的价格,-1终止循环。

我得到了

我在Eclipse中运行Java11。

在不使用该方法的情况下,代码运行良好,因为我减少了计数器,并且在循环的下一次迭代中,无论是否删除了以前的数组位置,都将覆盖它。方法本身被设置为删除ArrayList.size()-1,以便删除最后一个条目。我已经用-2和0试过了,但它仍然出界。

我通读了以前的问题,许多人没有填充数组。因此,我运行了一个打印存根,以确保ArrayList已经正确填充,并且它已经填充:当两个项被放置到ArrayList中时,大小等于2。错误代码也上升更多的项目,我放入代码,但总是项目-1索引超出边界的项目-1长度,我肯定我犯了一个菜鸟错误,但我找不到它,它是让我疯了!

对于完整的错误上下文:

线程“main”java.lang.indexoutOfboundsException:在java.base/jdk.internal.util.preconditions.outofbounds(preconditions.java:64),在java.base/jdk.internal.util.preconditions.outofboundscheckindex(preconditions.java:70),在java.base/jdk.internal.util.preconditions.checkindex(preconditions.java:248),在(c_m_ideaproject.java:76)

// declare our array list which will hold our prices!
ArrayList<Double> prices = new ArrayList<Double>();


// declaring variables to terminate loop, count prices, total prices, and current entry for conditions
        int counter  = 0;
        double entry = 0;
        double total = 0;

// our loop to continuously add prices to the array list from input
while (entry != -1) {

    System.out.println("Enter a price for item #" + (counter+1) + ": ");
    entry = myInput.nextDouble();

// if the entry is a price we will add it to prices and continue with the loop

    if (entry != 0 && entry != -1) {
            prices.add(entry);
            total += entry;
            counter++;
    }

//if the entry is 0 we will revert back to the previous iteration to re-add

    else if (entry == 0.0) {
        total -= prices.get(counter-1);
        removeLastEntry(prices);
        counter--;
    }

public static void removeLastEntry(ArrayList<Double> anArrayList) {
    anArrayList.remove(anArrayList.size()-1);
}

共有1个答案

赖鸿羲
2023-03-14

您可以添加一个检查以查看列表是否为空:

// declare our array list which will hold our prices!
ArrayList<Double> prices = new ArrayList<Double>();


// declaring variables to terminate loop, count prices, total prices, and current entry for conditions
        int counter  = 0;
        double entry = 0;
        double total = 0;

// our loop to continuously add prices to the array list from input
while (entry != -1) {

    System.out.println("Enter a price for item #" + (counter+1) + ": ");
    entry = myInput.nextDouble();

// if the entry is a price we will add it to prices and continue with the loop

    if (entry != 0 && entry != -1) {
            prices.add(entry);
            total += entry;
            counter++;
    }

//if the entry is 0 we will revert back to the previous iteration to re-add

    else if (entry == 0.0) {
        total -= prices.get(counter-1);
        removeLastEntry(prices);
        counter--;
    }

public static void removeLastEntry(ArrayList<Double> anArrayList) {
    if(!anArrayList.isEmpty()) {
        anArrayList.remove(anArrayList.size()-1);
    }
}
 类似资料:
  • 固定的: 我删除了while循环并添加了 我正在尝试向一个单词中随机添加一个额外的字母副本(介于0-8之间)。实例does可能会变成dooee或doess。 我的函数有时会工作,但总是会因越界索引错误而崩溃。 我假设我需要在某个时刻检查ArrayList中的空值。我试着用和if来包装我的while语句,以检查它,但没有任何改进。有什么建议吗?

  • 问题内容: 在我的游戏代码中,我尝试添加一张手牌。一旦我做完了,我的数组就超出了范围。一切看起来都不错,但也许我缺少了一些东西。 仅供参考,一个和两个是Player实例。来自Main类的相关代码(对格式感到抱歉。我很想将其传输到Stack Overflow): 卡类: 玩家等级: 问题答案: 问题出在你的循环上 没有其他任何值可设置,因此此循环不断循环,直到所有玩家拥有超过52张牌为止。一旦某人拥

  • 我犯的错误 我的密码 我试图将数组的最后5个元素存储在temp 2中。那我就换了。有更简单的方法吗?将数组的前五个元素与后五个元素切换?你会如何用for循环切换它们?

  • 所以基本上我应该做的是,我应该得到一个字符串的数组列表,用两个字符串填充它,然后比较它们。例如,如果一个字符串是“1,2,3,4”,第二个字符串是“7,6,2,8,1”,那么它应该打印出“1,2”,因为它打印出的数字相似。但我遇到了和arrayindexoutofbounds异常,我不知道如何修复它。此外,如果你有任何可能缩短此代码的提示,也请告诉我。我有一个习惯,让我的代码超长。

  • 我知道这个问题以前可能被问过很多次,但我有一个问题,我找不到一个特别针对我的问题的解决方案。 问题如下: 我有两个火箭和射弹的阵列清单,它们之间的碰撞被检查。当它们碰撞时,两者都被移除。问题是,当arraylist中唯一的火箭被击中时,异常发生了,我不知道如何阻止它发生。 碰撞方法+ArrayLists: 删除和添加方法: 注意:rocket.die()基本上只是删除火箭,与remove方法的方式

  • 我不明白为什么这个方法不起作用。它会编译,但会引发运行时错误。