I’m getting the following error:
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
I’ve looked the code over many times and I can’t seem to find where I going over the index of the arraylist…
Here is the code for the 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;
}
And my main method (in another class) just in case:
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));
}
}
}
It should just print out the elements of the dotProduct ArrayList…
I don’t understand how line 72 is causing a problem since I should be able to add an unlimited number of items to the ArrayList….Any help would be appreciated.
The issue in line 72 is with a
get(), not with anadd().I suspect this could be the root cause of the problem:
In the second for loop, you’re incrementing
iand notj. That will probably result in you using a value foriin the linelarger than the size of
pureRatingsList, resulting in the exception you’re seeing.