Я пытаюсь прочитать список данных целых чисел, найти самые популярные, наименее популярным, и в среднем, и сообщить следующее ...Java: указатель за пределы при использовании ArrayList?
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)
И я не могу понять, почему. Я не думал, что мне нужно беспокоиться о outofboundsexceptions при использовании ArrayList? О, и это мой первый раз, используя ArrayList, поэтому, если мой код крайне уродлив, я прошу прощения. Любая помощь очень ценится!
Я изменил все функции с помощью total-1, чтобы исправить это, но я все равно получаю ту же ошибку? – user102817
@ user102817 Обновите свой последний код. –
Теперь обновлено – user102817