-3

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?

TomD
  • 9
  • 1
  • 7
  • 2
    not an answer to your question, but what do you think happens when you call list.get(x) on a linked list? How many operations does it take to retrieve your item? What happens when you have a list with 1000 elements and you do a list.get(x) on each one of these 1000 items? How many operations is that? – Nick Hristov Dec 11 '17 at 03:04
  • 1
    This question is not useful to other, so please delete it before you get upvoted answers that'll prevent you from deleting it. – Andreas Dec 11 '17 at 03:11

2 Answers2

0

You've got an i instead of a j in your for-loop condition:

for (int j = 0; i < customerList.size(); j++) {

That should be:

for (int j = 0; /* Note the j here --> */ j < customerList.size(); j++) {
Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
  • Wow. I've been coding for 8 hours straight and this is what has got me stuck for an hour.... Thank you, I feel stupid now... – TomD Dec 11 '17 at 03:07
  • 2
    Although your answer is correct and helpful, the question itself is not generally helpful, as it is a simple "typo". The question should be deleted, but OP cannot delete the question if there is an up-voted answer, so I've down-vote it. You should delete the answer and leave a comment instead. – Andreas Dec 11 '17 at 03:08
0

To answer your question, your bug is here:

int j = 0; i < customerList.size(); j++

You are comparing i instead of j.

Name your iteration variables better. idx, innerIdx, etc.

Also, note my comment above. Do not use get() method on a linked list: rather, use an iterator, or implicit java 8 Iterable syntax (for x : xcollection) { ... }. This is a O(n) operation!

Nick Hristov
  • 905
  • 1
  • 8
  • 17