Reputation: 53
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.
Upvotes: 1
Views: 2401
Reputation: 19766
isn't this line the problem?
for (int j = 0; j < bookList.size(); i++) {
I guess what you need is
for (int j = 0; j < bookList.size(); j++) {
Upvotes: 2
Reputation: 7144
The issue in line 72 is with a get()
, not with an add()
.
I suspect this could be the root cause of the problem:
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);
}
In the second for loop, you're incrementing i
and not j
. That will probably result in you using a value for i
in the line
sum = sum + (pureRatingsList.get(index).get(j))
* (pureRatingsList.get(i).get(j));
larger than the size of pureRatingsList
, resulting in the exception you're seeing.
Upvotes: 6
Reputation: 76898
It's causing a problem because you're requesting an index that doesn't exist; ergo "Out of bounds".
You're requesting index 86 when the size is only 86 (which is index 0 - 85). Arrays are zero based.
Learning to use the debugger will really help you solve problems like this as you can step through your program and see exactly what's happening.
Upvotes: 0
Reputation: 7655
You do know that there are things like iterators and foreach to make traversing a collection simpler?
The problem is that the index of a list starts at 0 and you try to start at 1.
Upvotes: 1