【问题标题】:Java: Index out of bounds while using ArrayList?Java:使用 ArrayList 时索引超出范围?
【发布时间】:2014-03-10 01:55:28
【问题描述】:

我正在尝试通读整数数据列表,找出最受欢迎、最不受欢迎和平均水平,并报告以下内容...

MOST POPULAR NUMBERS
The following numbers were picked 263 times: 41

LEAST POPULAR NUMBERS
The following numbers were picked 198 times: 20

AVERAGE
The Average was 228.545455 times.
The following numbers were picked 228 times:  5 22
The following numbers were picked 229 times:  2  7 12 40

我的代码...

import java.util.*;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
public class Hmwk {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner input=new Scanner (new File ("input.txt"));
        int counter = 0;
        ArrayList<Integer> numberList = new ArrayList<Integer>(45);
        while(input.hasNextInt()){
            int in = input.nextInt();
            numberList.add(in);
            counter++;
        }
        mostPopular(numberList,counter);
        leastPopular(numberList,counter);
        average(numberList,counter);


    }
public static void mostPopular(ArrayList<Integer> list, int total){
    Collections.sort(list);
    int popular = 0;
    int counter = 0;
    int counterTwo = 0;
    for (int i=0; i<total-1; i++){
        while(list.get(i) == list.get(i+1)){
            counter++;
            i++;
        }
        if(counter > counterTwo){
            counterTwo = counter;
            popular = i;
        }
    }
    System.out.printf("MOST POPULAR NUMBERS");
    System.out.printf("The following number was picked",counterTwo,"times:", popular);

}   
public static void leastPopular(ArrayList<Integer> list, int total){
    Collections.sort(list);
    int unpopular=0;
    int counter = 0;
    int counterTwo = 0;
    for (int i=0; i<total-1; i++){
        while(list.get(i) == list.get(i+1)){
            counter++;
            i++;

        if(counter < counterTwo){
            counterTwo = counter;
            unpopular = i;
        }
        }

    }
    System.out.printf("LEAST POPULAR NUMBERS");
    System.out.printf("The following number was picked",counterTwo,"times:", unpopular);
}

public static void average(ArrayList<Integer> list, int total){
    int sum = 0;
    int counter = 0;
    ArrayList<Integer> average = new ArrayList<Integer>(45);
    for (int i=0; i<total-1; i++){
        while(list.get(i) == list.get(i+1)){
            counter++;
            i++;
        }
        average.add(counter);
    }


    for (int i = 0; i <average.size(); i++){
        sum+= average.get(i);
    }
    double average2 = sum/total;
    System.out.printf("AVERAGE");
    System.out.printf("The Average was",average,"times.");
    double ceiling = Math.ceil(average2) ;
    double floor = Math.floor(average2);
    int counter2 = 0;
    Collections.sort(list);
    for (int i=0; i<total-1; i++){
        while(list.get(i) == list.get(i+1)){
            counter2++;
            i++;
        }
        if(counter2 == ceiling){
            System.out.printf("The following number was picked", ceiling,"times:",i);
        }
        if (counter2 == floor){
            System.out.printf("The following number was picked", floor,"times:",i);
    }


    }   

}

我得到了错误...

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2555, Size: 2555
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Hmwk.mostPopular(Hmwk.java:31)
    at Hmwk.main(Hmwk.java:19)

我似乎无法弄清楚为什么。我认为使用 ArrayList 时不需要担心 outofboundsexceptions?哦,这是我第一次使用 ArrayList,所以如果我的代码非常难看,我很抱歉。非常感谢任何和所有帮助!

【问题讨论】:

    标签: java for-loop arraylist while-loop indexoutofboundsexception


    【解决方案1】:

    在每个函数中,你有:

    for (int i=0; i<total; i++){
    while(list.get(i) == list.get(i+1)){
            counter++;
            i++;
        }
    

    并且在while语句中你将i增加i++,所以当list.get(i) == list.get(i+1)时,它会导致异常。你必须检查i在while语句中的值:

       while(list.get(i) == list.get(i+1)){
           counter++;
           i++;
           if(i == total-1) break;
       }
    

    如果i = max(i = total) 那么i+1(i = total + 1) 将导致异常。

    【讨论】:

    • 我用 total-1 更改了所有函数来解决这个问题,但我仍然遇到同样的错误?
    • @user102817 看我的回答。
    • 我将如何修复我的 while 循环?我会改用 ++i 吗?还是将 i++ 放在循环中的其他位置?
    • 或者如何在不引起异常的情况下增加 i?
    【解决方案2】:

    您需要将 for 循环更改为

    for (int i=0; i<total-1; i++){
    

    否则尝试访问 [i+1] 将导致异常。

    我认为使用 ArrayList 时不需要担心 outofboundsexceptions

    是的,您在访问特定索引时会这样做。只有当您向 ArrayList 添加内容时,您才无需担心大小,这与使用普通数组时不同。

    【讨论】:

      【解决方案3】:

      在最后一次迭代中,您尝试在数组外的索引上使用 get。

      int counterTwo = 0;
      for (int i=0; i<total; i++){
          while(list.get(i) == list.get(i+1)){
      

      假设total = 10 这意味着数组来自0-9,而不是当我们在最后一次迭代中使用i = 9 时,操作.get(i+1) 导致.get(10) == 异常!

      修复:适当的修复将停止数组前一个索引。
      变化:

      for (int i=0; i<total; i++){
      

      有了这个:

      for (int i=0; i<total-1; i++){
      

      【讨论】:

      • 我什至没有想到这一点,但我对所有 for 循环都这样做了,但我仍然遇到同样的错误
      猜你喜欢
      • 2021-12-10
      • 2019-03-25
      • 2017-07-30
      • 2020-02-13
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多