,我发现了以下错误: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 ....任何帮助,将不胜感激。
指数是基于从0。您正在访问第87个项目(索引86)以获得86个项目的列表。这很可能是你的索引变量搞砸了。 (例如,你从[0,'customerList.size'中迭代'i',但在'pureRatingsList.get'中使用'i' ...正确?) – 2012-02-03 07:53:38
你是否尝试过使用调试器?在java.lang.IndexOutOfBoundsException上放置一个断点并查看它为什么会中断。 – Axel 2012-02-03 07:55:18