I'm not exactly sure how an IndexOutOfBoundsException would be thrown when going through a LinkedList. I have this method I'm working on and apparently it's giving me the error on the last element I'm on in the second for loop. This happens on the 'int pid = customerList.get(j);' line
public double averageRating() {
// Creates new instance of ProductArrayList, reading in from the .txt file
// two variables for calculating average
double count = 0;
double average = 0;
// Nested for-loop to cycle through each customer, grab their productHistory,
// add it, set their ranking and count
// what ranking they are.
for (int i = 0; i < listOfCustomers.size(); i++) {
double total = 0;
LinkedList<Integer> customerList = purchaseHistory.get(i);
for (int j = 0; i < customerList.size(); j++) {
double tempTotal = 0;
int pid = customerList.get(j);
Product tempProduct = listOfProducts.get(pid);
tempTotal = tempProduct.getPrice();
total = tempTotal + total;
}
if (total <= 0) {
listOfCustomers.get(i).setRank(0);
count = count + 0;
}
if (total > 0 && total < 150) {
listOfCustomers.get(i).setRank(1);
count = count + 1;
}
if (total >= 150 && total < 300) {
listOfCustomers.get(i).setRank(2);
count = count + 2;
}
if (total >= 300 && total < 450) {
listOfCustomers.get(i).setRank(3);
count = count + 3;
}
if (total >= 450 && total < 600) {
listOfCustomers.get(i).setRank(4);
count = count + 4;
}
if (total >= 600 && total < 750) {
listOfCustomers.get(i).setRank(5);
count = count + 5;
}
if (total >= 750) {
listOfCustomers.get(i).setRank(6);
count = count + 6;
}
}
// Calculate the average based off the count added and the total amount of
// customers.
average = count / listOfCustomers.size();
System.out.println("The average ranking is: " + average);
return average;
}
This produces an error;
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 13, Size: 13, at java.util.LinkedList.checkElementIndex(Unknown Source), at java.util.LinkedList.get(Unknown Source), at teama.arraylist.CustomerArrayList.averageRating(CustomerArrayList.java:305) at teama.Sandbox.main(Sandbox.java:20)
It seems like I'm within the range? Any suggestions on why this is happening and how I can prevent it in the future? How do I fix this?