簡體   English   中英

Java:使用ArrayList時索引超出范圍?

[英]Java: Index out of bounds while using 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)

而且我似乎無法弄清楚原因。 我不認為我在使用ArrayList時不必擔心outboundsexceptions嗎? 哦,這是我第一次使用ArrayList,所以如果我的代碼非常難看,我深表歉意。 任何幫助都將不勝感激!

在最后一次迭代中,您嘗試對數組外的索引使用get

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

假設total = 10 ,這意味着數組在0-90-9比在i = 9的動作.get(i+1)產生的.get(10) ==異常!

修復:適當的修復將停止數組在其之前的一個索引。
更改:

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

有了這個:

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

在每個函數中,您都有:

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) ,它將導致異常。您必須在while語句中檢查i值:

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

如果i = max (i =總數),則i+1 (i =總數+ 1)將導致異常。

您需要將for循環更改為

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

否則嘗試訪問[i + 1]將導致異常。

我不認為我在使用ArrayList時不必擔心outboundsexceptions

是的,您在訪問特定索引時會這樣做。 與使用普通Array相比,僅當您向ArrayList添加內容時,您才不必擔心大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM