1

次のエラーが発生します。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 86, Size: 86
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at Netbooks.Recommendations.getDotProduct(Recommendations.java:72)
at Netbooks.TestRecomendations.main(TestRecomendations.java:11)
Java Result: 1

コードを何度も調べましたが、配列リストのインデックスをどこで調べているのかわかりません...

dotProductArrayListのコードは次のとおりです。

public List<Integer> getDotProduct() throws IOException {
    Books book = new Books();
    Ratings cust = new Ratings();
    PureRatings pureRatings = new PureRatings();


    List<String> bookList = book.readBooks();
    List<String> customerList = cust.readCustomers();
    List<List<Integer>> pureRatingsList = pureRatings.parseRatingsFile();
    List<Integer> dotProduct = new ArrayList<Integer>();
    int index = getCustIndex();

    if (index == -1) {
        return dotProduct;
    }

    for (int i = 0; i < customerList.size(); i++) {
        int sum = 0;

        for (int j = 0; j < bookList.size(); i++) {
            if (i == index) {
                dotProduct.add(0);
            } else { //Next line is line 72.
                sum = sum + (pureRatingsList.get(index).get(j)) * (pureRatingsList.get(i).get(j)); //Line 72.
            }
        }
        dotProduct.add(sum);
    }

    return dotProduct;
}

そして、念のために私のメインメソッド(別のクラス):

public class TestRecomendations {

    public static void main(String[] args) throws IOException {
        Recommendations recomm = new Recommendations();

        List<Integer> dotProduct = recomm.getDotProduct();//Line 11.

        for (int i = 0; i < dotProduct.size(); i++) {
            System.out.println(dotProduct.get(i));
        }
    }
}

dotProductArrayListの要素を出力するだけです...

ArrayListに無制限の数の項目を追加できるはずなので、72行目がどのように問題を引き起こしているのかわかりません。

4

4 に答える 4

6

72行目の問題は、get()ではなく、にありadd()ます。

これが問題の根本的な原因である可能性があると思います。

for (int i = 0; i < customerList.size(); i++) {
    int sum = 0;

    for (int j = 0; j < bookList.size(); i++) {  
        if (i == index) {
            dotProduct.add(0);
        } else { //Next line is line 72.
            sum = sum + (pureRatingsList.get(index).get(j)) * (pureRatingsList.get(i).get(j)); //Line 72.
        }
    }
    dotProduct.add(sum);
}

2番目のforループでは、インクリメントしているのであって、ではiありませんj。その結果i、行にの値を使用することになります。

sum = sum + (pureRatingsList.get(index).get(j)) 
     * (pureRatingsList.get(i).get(j));

のサイズよりも大きいため、表示さpureRatingsListれている例外が発生します。

于 2012-02-03T07:54:20.887 に答える
2

この線は問題ではありませんか?

for (int j = 0; j < bookList.size(); i++) {

必要なのは

for(int j = 0; j <bookList.size(); j ++){

于 2012-02-03T08:12:40.150 に答える
1

コレクションのトラバースを簡単にするためのイテレーターやforeachのようなものがあることをご存知ですか?

問題は、リストのインデックスが0から始まり、1から始めようとすることです。

于 2012-02-03T07:51:49.083 に答える
0

存在しないインデックスを要求しているため、問題が発生しています。エルゴ「範囲外」。

サイズが86(インデックス0〜85)しかない場合は、インデックス86を要求しています。配列はゼロベースです。

デバッガーの使用法を学ぶことは、プログラムをステップスルーして何が起こっているかを正確に確認できるため、このような問題を解決するのに本当に役立ちます。

于 2012-02-03T07:54:06.363 に答える