-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);
                            }
                        }
                    }

1 Answers1

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.

tremendous7
  • 721
  • 6
  • 9
  • 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 May 16 '21 at 13:42
  • @maarcoscuesta18 can you report what error will be thrown? – tremendous7 May 16 '21 at 14:03
  • with your code ```for (mercadonaProducts product : new ArrayList(mercadonaCartProducts)) {``` appear this--> Required type: Object Provided: mercadonaProducts – maarcoscuesta18 May 16 '21 at 14:17
  • @maarcoscuesta18 you are right. i updated the answer – tremendous7 May 16 '21 at 14:23
  • 1
    @maarcoscuesta18: you simply needed `new ArrayList(...)` instead. Avoid [raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Joachim Sauer May 16 '21 at 14:30
  • we are in the same situation the code throw java.uti.currentModificationException, so actually i have doubts if i write your code right or not or i didnt understand ur idea – maarcoscuesta18 May 16 '21 at 14:33