简体   繁体   中英

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? Oh and this is my first time using ArrayList so if my code is extremely ugly, I apologize. Any and all help is much appreciated!

At your last iteration you are trying to use get on an index out of the array.

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!

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(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.

Your need to change your for loop to

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

otherwise trying to access [i+1] will result in the exception.

I didn't think I needed to worry about outofboundsexceptions when using ArrayList

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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