-2

I am coding a cart and when i add a product that are in the arraylist the app throw a execption, and i dont know why. So, this is the code what im using right now. Any help?

//the arraylist is not empty

public static ArrayList<mercadonaProducts> mercadonaCartProducts = new ArrayList<>();


if(mercadonaCartProducts.isEmpty()){
               mercadonaCartProducts.add(new mercadonaProducts(product_name.getText().toString(),price.getText().toString().substring(7,11),String.valueOf(count[0]),String.valueOf(Double.parseDouble(price.getText().toString().substring(7,11))*count[0])));
                        saveCart(v);
                    }else{
                        for(mercadonaProducts product : mercadonaCartProducts){
                            if(product.getCartproduct_name().equals(product_name.getText().toString())){
                                product.setQty(String.valueOf(Integer.parseInt(product.getQty())+count[0]));
                                product.setTotalprice(Double.parseDouble(product.getQty())*product.getCartprice());
                                saveCart(v);
                            }else{
                                mercadonaCartProducts.add(new mercadonaProducts(product_name.getText().toString(),price.getText().toString().substring(7,11),String.valueOf(count[0]),String.valueOf(Double.parseDouble(price.getText().toString().substring(7,11))*count[0])));
                                saveCart(v);
                            }
                        }
                    }
2
  • i dont think so, i dont want to use iterators cuz i want to modify the object data in that position of the arraylist, with iterators i cant do it i think Commented May 16, 2021 at 11:41
  • You add to mercadonaCartProducts while iterating over it. That's a no-no. Commented May 16, 2021 at 14:28

1 Answer 1

0

here

for (mercadonaProducts product : mercadonaCartProducts) {

you are iterating over products of mercadonaCartProducts

in the loop in the else branch you invoke

mercadonaCartProducts.add(new mercadonaProducts(...));

so while iterating over the list you are also adding new elements in the list.

to avoid the ConcurrentModificationException change the for instruction into

ArrayList<mercadonaProducts> mercadonaCartProductsCopy = new ArrayList(mercadonaCartProducts);
for (mercadonaProducts product : mercandonaCartproductsCopy) {

this will be enough if there is no concurrency involved.

Sign up to request clarification or add additional context in comments.

6 Comments

you mean that in the for each declaration change the for (mercadonaProducts product : mercadonaCartProducts) { for for (mercadonaProducts product : new ArrayList(mercadonaCartProducts)) { ??? I think that this isn't going to work no? i mean the foreach will throw a error because need a object and that declaration doesnt work
@maarcoscuesta18 can you report what error will be thrown?
with your code for (mercadonaProducts product : new ArrayList(mercadonaCartProducts)) { appear this--> Required type: Object Provided: mercadonaProducts
@maarcoscuesta18 you are right. i updated the answer
@maarcoscuesta18: you simply needed new ArrayList<mercadonaProducts>(...) instead. Avoid raw types.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.