2021年4月4日星期日

ForEach loop in ForEach loop arraylist crash [duplicate]

I am trying to separate items from array list of custom class and have two foreach loop.

I want to add the quantity if the variants and id of the item are same and if if it is not then I want to add new entry into the arraylist.

I am able to separate items successfully but it crashes after adding 2 items in a new arraylist. Sometimes it shows concuurentModification exception and sometime it crashes just by showing shutting VM.

Please suggest, I tried listIterator but it is not helping out with 2 foreach loops.

Thanks in advance.

List<OrderedItem> getSortedOrderList(List<OrderEcomItem> orderEcomItems, Context context) {      List<OrderedItem> sorted = new ArrayList<>();        for (OrderEcomItem item : orderEcomItems) {          final ItemNode itemNode = item.getNodePresentation();          String variants = "";          String description = "";          if (!item.isInStoreItem()) {              variants    = itemNode.selectedItems();              description = itemNode.getValue().getDesc();          }            final int id = item.getEcomItemId();          if(sorted.size() > 0) {              for (OrderedItem order : sorted) {                  if (order.getEcomItemId() == id) {                      if (!order.getGroups().isEmpty()                           && order.getGroups().equals(variants)) {                          order.setQuantity(order.getQuantity()                               + item.getOrderItemQuantity()                          );                          order.setAmount(order.getAmount()                               + Utils.getSubtotal(item)                          );                      } else {                          sorted.add(new OrderedItem(                              item.getName(),                               item.getOrderItemQuantity(),                              Utils.getSubtotal(item),                              item.getEcomItemId(), variants, description,                              item.getExternalId()                          ));                      }                  } else {                      sorted.add(new OrderedItem(                          item.getName(), item.getOrderItemQuantity(),                          Utils.getSubtotal(item),                          item.getEcomItemId(), variants, description,                          item.getExternalId()                      ));                  }              }          } else {              sorted.add(new OrderedItem(                  item.getName(), item.getOrderItemQuantity(),                  Utils.getSubtotal(item), item.getEcomItemId(),                   variants, description, item.getExternalId()              ));          }      }      return sorted;  }  

Following is the iterator approach but it is not giving me desired result. It is not separating items and not adding items into arraylist too.

List<OrderedItem> getSortedOrderList(List<OrderEcomItem> orderEcomItems, Context context) {  List<OrderedItem> sorted= new ArrayList<>();  ListIterator<OrderedItem> iterator= sorted.listIterator();  for (OrderEcomItem item : orderEcomItems) {    final ItemNode itemNode = item.getNodePresentation();    String variants = "";    String description = "";    if (!item.isInStoreItem()) {      variants = itemNode.selectedItems();      description = itemNode.getValue().getDesc();     }      final int id = item.getEcomItemId();    if(sorted.size()>0) {      while (iterator.hasNext()){        OrderedItem order= iterator.next();        if (order.getEcomItemId() == id) {          if (!order.getVariants().isEmpty() && order.getVariants().equals(variants)) {            order.setQuantity(order.getQuantity() + item.getOrderItemQuantity());            order.setAmount(order.getAmount() + Utils.getSubtotal(item));          } else {            sorted.add(                new OrderedItem(item.getName(), item.getOrderItemQuantity(),                    item.getEcomItemId(), groups, description,                    item.getExternalId()));          }        }else {          sorted.add(              new OrderedItem(item.getName(), item.getOrderItemQuantity(),                  item.getMenuItemId(), groups, description,                  item.getExternalId());        }      }    }else {      sorted.add(          new OrderedItem(item.getName(), item.getOrderItemQuantity(),              item.getEcomItemId(), variants, description,              item.getExternalId()));    }  }  return sorted;  

}

https://stackoverflow.com/questions/66946029/foreach-loop-in-foreach-loop-arraylist-crash April 05, 2021 at 05:28AM

没有评论:

发表评论