CSA: Unit 8 Lessonn 7 Exercise 6(a)

This is my solution to the problem, but when I test it I get an IndexOutOfBounds Exception. I compared it to the teacher’s solution and it seems that the only difference is that in my solution I traverse backwards. Can someone tell me where I’ve gone wrong?

public static void combineSales(ArrayList combined)
{
/* ----------------------------------- TO DO -----------------------------------
* Find duplicate products and combine their total quantities sold. Remove
* the duplicate products from the combined list.
* -----------------------------------------------------------------------------
*/

``````  for(int o = combined.size() - 1; o > -1; o--)
{
for(int c  = o-1; c > -1; c--)
{
Product p = combined.get(o);
Product p2 = combined.get(c);
if(p.equals(p2))
{
p.setQuantity(p.getQuantity() + p2.getQuantity());
combined.remove(c);
}
}
}
``````

}

Hi @bavarians! Thank you for posting your question to the forum.

I copied and pasted your code the method combineSales() and the teacher solution code for combineWeeks() and I am not generating and indexOutOfBounds exception. This leads me to believe that the problem is not with your combineSales() method.

Could you share the link to this level so I can look at it further?

Best
-Sam