gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:27:54 27 4
gpt4 key购买 nike

我正在尝试读取整数数据列表,找到最流行的、最不流行的和平均值,并报告以下内容...

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,所以如果我的代码非常难看,我深表歉意。非常感谢任何和所有的帮助!

最佳答案

在最后一次迭代中,您尝试对数组外的索引使用 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++){

关于Java:使用 ArrayList 时索引超出范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21716102/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com