我收到以下错误:
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 添加无限数量的项目......任何帮助将不胜感激。