2012-02-03 178 views
1

,我發現了以下錯誤:OutOfBoundsException煩惱,Java的

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 

我看過了很多次的代碼,我似乎無法找到在那裏我渡過了數組列表的索引.. 。

下面是dotProduct的ArrayList代碼:

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)); 
     } 
    } 
} 

它應該只是打印出dotProduct ArrayList中的元素...

我不明白怎麼行72造成一個問題,因爲我應該能夠項目的數量不受限制添加到ArrayList ....任何幫助,將不勝感激。

+1

指數是基於從0。您正在訪問第87個項目(索引86)以獲得86個項目的列表。這很可能是你的索引變量搞砸了。 (例如,你從[0,'customerList.size'中迭代'i',但在'pureRatingsList.get'中使用'i' ...正確?) – 2012-02-03 07:53:38

+3

你是否嘗試過使用調試器?在java.lang.IndexOutOfBoundsException上放置一個斷點並查看它爲什麼會中斷。 – Axel 2012-02-03 07:55:18

回答

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); 
} 

在第二個for循環,你遞增i,而不是j。在該行

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

pureRatingsList規模較大的使用i值,導致你所看到的異常可能會導致你。

+1

(+1)斑點! – NPE 2012-02-03 07:55:50

+0

謝謝!剛剛用j替換了我,現在運行良好。它總是簡單的錯誤... – Marcos 2012-02-03 07:59:27

1

你知道有像迭代器和foreach的東西使遍歷集合更簡單嗎?

的問題是,列表的索引從0開始,並嘗試從1開始

+0

今天我幾乎沒有瞭解迭代器,並且不明白它們足夠有效地使用它們(儘管我肯定會研究它們)...我在哪裏開始索引1? – Marcos 2012-02-03 07:56:26

0

因爲你請求不存在索引它引起的問題; ergo「越界」。

當請求索引86時,大小隻有86(索引0 - 85)。數組基於零。

學習如何使用調試器將幫助您解決這樣的問題,因爲您可以逐步完成程序並查看到底發生了什麼。

2

是不是這行問題?

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

我猜你需要的是

爲(INT J = 0;Ĵ< bookList.size(); Ĵ ++){