简体   繁体   English

Java:使用ArrayList时索引超出范围?

[英]Java: Index out of bounds while using ArrayList?

I'm trying to read through a data list of integers, find the most popular, least popular, and average and report the following... 我正在尝试通读整数数据列表,找到最流行,最不流行的平均值,并报告以下内容...

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

My code... 我的代码...

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


    }   

}

I'm getting the error... 我遇到了错误...

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)

And I can't seem to figure out why. 而且我似乎无法弄清楚原因。 I didn't think I needed to worry about outofboundsexceptions when using ArrayList? 我不认为我在使用ArrayList时不必担心outboundsexceptions吗? Oh and this is my first time using ArrayList so if my code is extremely ugly, I apologize. 哦,这是我第一次使用ArrayList,所以如果我的代码非常难看,我深表歉意。 Any and all help is much appreciated! 任何帮助都将不胜感激!

At your last iteration you are trying to use get on an index out of the array. 在最后一次迭代中,您尝试对数组外的索引使用get

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

Let's say total = 10 that means the array is from 0-9 than when we are at the very last iteration you are using at i = 9 the action .get(i+1) resulting with .get(10) == Exception! 假设total = 10 ,这意味着数组在0-90-9比在i = 9的动作.get(i+1)产生的.get(10) ==异常!

Fix: A proper fix will to stop the array one index before it. 修复:适当的修复将停止数组在其之前的一个索引。
Change: 更改:

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

With this: 有了这个:

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

In each function, you have: 在每个函数中,您都有:

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

and in while statement you increase i by i++ , so when list.get(i) == list.get(i+1) , it will cause an exception.You have to check i value inside while statement: while语句中,您将i加i++ ,因此当list.get(i) == list.get(i+1) ,它将导致异常。您必须在while语句中检查i值:

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

And if i = max (i = total) then i+1 (i = total + 1) will cause an exception. 如果i = max (i =总数),则i+1 (i =总数+ 1)将导致异常。

Your need to change your for loop to 您需要将for循环更改为

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

otherwise trying to access [i+1] will result in the exception. 否则尝试访问[i + 1]将导致异常。

I didn't think I needed to worry about outofboundsexceptions when using ArrayList 我不认为我在使用ArrayList时不必担心outboundsexceptions

yes, you do when accessing a specific index. 是的,您在访问特定索引时会这样做。 Only when you are adding stuff to the ArrayList you don't need to worry about the size, in contrast to when you use normal Arrays. 与使用普通Array相比,仅当您向ArrayList添加内容时,您才不必担心大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM