2012-02-03 172 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(); Ĵ ++){