如何避免在此问题上出现此错误java.util.ConcurrentModificationException?

时间:2019-05-05 18:26:43

标签: java arraylist

我有这段代码希望更改arraylist的内容,我已经看到了使用Iterators的可行解决方案,但是如何将其添加到这种代码中呢?主要似乎是问题在于for循环。似乎在循环中我无法执行任何添加或删除方法。有没有解决的办法?

            Stream<String> stream = Arrays.stream(lineas);
            long contOp = stream.filter(op -> op.startsWith("*") || op.startsWith("/")).count();
            for (long i = 0; i < contOp; i++) {
                if (lstRest.contains("*") || lstRest.contains("/")) {
                    if ((lstRest.indexOf("/") > lstRest.indexOf("*")) && lstRest.indexOf("*") != -1) {
                        if (!lstTemp.isEmpty()) {
                            for (int j = 0; j < lstRest.indexOf("*") + 1; j++) {
                                lstTemp.add(lstRest.get(j));
                            }
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("*") + 1, lstRest.size());
                            System.out.println(lstRest);
                        } else {
                            lstTemp = lstRest.subList(0, lstRest.indexOf("*") + 1);
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("*") + 1, lstRest.size());
                            System.out.println(lstRest);
                        }

                    } else if ((lstRest.indexOf("/") < lstRest.indexOf("*")) && lstRest.indexOf("/") != -1) {
                        if (!lstTemp.isEmpty()) {
                            for (int j = 0; j < lstRest.indexOf("/") + 1; j++) {
                                lstTemp.add(lstRest.get(j));
                            }
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("/") + 1, lstRest.size());
                            System.out.println(lstRest);
                        } else {
                            lstTemp = lstRest.subList(0, lstRest.indexOf("/") + 1);
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("/") + 1, lstRest.size());
                            System.out.println(lstRest);
                        }
                    } else if (lstRest.indexOf("/") == -1) {
                        if (!lstTemp.isEmpty()) {
                            for (int j = 0; j < lstRest.indexOf("*") + 1; j++) {
                                lstTemp.add(lstRest.get(j));
                            }
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("*") + 1, lstRest.size());
                            System.out.println(lstRest);
                        } else {
                            lstTemp = lstRest.subList(0, lstRest.indexOf("*") + 1);
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("*") + 1, lstRest.size());
                            System.out.println(lstRest);
                        }
                    } else if (lstRest.indexOf("*") == -1) {
                        if (!lstTemp.isEmpty()) {
                            for (int j = 0; j < lstRest.indexOf("/") + 1; j++) {
                                lstTemp.add(lstRest.get(j));
                            }
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("/") + 1, lstRest.size());
                            System.out.println(lstRest);
                        } else {
                            lstTemp = lstRest.subList(0, lstRest.indexOf("/") + 1);
                            System.out.println(lstTemp);
                            lstRest = lstRest.subList(lstRest.indexOf("/") + 1, lstRest.size());
                            System.out.println(lstRest);
                        }
                    }
                }
                System.out.println(contOp);
            }

0 个答案:

没有答案