Java java.util.ConcurrentModificationException错误

vij*_*jay 2 java core

请允许任何人帮我解决这个问题,这么多天我无法解决这个错误.我尝试使用同步方法和其他方法,但没有工作,所以请帮助我

错误

java.util.ConcurrentModificationException
 at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
 at java.util.AbstractList$Itr.remove(Unknown Source)
 at JCA.startAnalysis(JCA.java:103)
 at PrgMain2.doPost(PrgMain2.java:235)
Run Code Online (Sandbox Code Playgroud)

 public synchronized void startAnalysis() {
        //set Starting centroid positions - Start of Step 1
        setInitialCentroids();
        Iterator<DataPoint> n = mDataPoints.iterator();
        //assign DataPoint to clusters
        loop1:
        while (true) {
            for (Cluster c : clusters)
            {
                c.addDataPoint(n.next());
                if (!n.hasNext())
                    break loop1;
            }
        }

        //calculate E for all the clusters
        calcSWCSS();

        //recalculate Cluster centroids - Start of Step 2
        for (Cluster c : clusters) {
            c.getCentroid().calcCentroid();
        }

        //recalculate E for all the clusters
        calcSWCSS();


       // List copy = new ArrayList(originalList);

        //synchronized (c) {

        for (int i = 0; i < miter; i++) {
            //enter the loop for cluster 1

         for (Cluster c : clusters) {


                for (Iterator<DataPoint> k = c.getDataPoints().iterator(); k.hasNext(); ) {
             //    synchronized (k) {

                 DataPoint dp = k.next(); 


                    System.out.println("Value of DP" +dp);
                    //pick the first element of the first cluster
                    //get the current Euclidean distance
                    double tempEuDt = dp.getCurrentEuDt();
                    Cluster tempCluster = null;
                    boolean matchFoundFlag = false;

                    //call testEuclidean distance for all clusters
                    for (Cluster d : clusters) {

                        //if testEuclidean < currentEuclidean then
                        if (tempEuDt > dp.testEuclideanDistance(d.getCentroid())) {
                            tempEuDt = dp.testEuclideanDistance(d.getCentroid());
                            tempCluster = d;
                            matchFoundFlag = true;
                        }
                        //if statement - Check whether the Last EuDt is > Present EuDt

                    }
                    //for variable 'd' - Looping between different Clusters for matching a Data Point.
                    //add DataPoint to the cluster and calcSWCSS

                    if (matchFoundFlag) {
          tempCluster.addDataPoint(dp);

         //k.notify();  
     //     if(k.hasNext())
          k.remove();


          for (Cluster d : clusters) {
                            d.getCentroid().calcCentroid();
                        }

                        //for variable 'd' - Recalculating centroids for all Clusters

                        calcSWCSS();
                  }

                    //if statement - A Data Point is eligible for transfer between Clusters.
                // }// syn
                 }                 
                //for variable 'k' - Looping through all Data Points of the current Cluster.
            }//for variable 'c' - Looping through all the Clusters.
        }//for variable 'i' - Number of iterations.
     // syn
    }
Run Code Online (Sandbox Code Playgroud)

pol*_*nts 8

您在迭代时无法修改列表,除非您通过该列表执行此操作Iterator.

来自API: ConcurrentModificationException

当不允许这样的修改时,检测到对象的并发修改的方法可能抛出此异常.

例如,一个线程通常不允许修改Collection另一个线程迭代它.

你的代码很乱,所以很难弄清楚发生了什么,但我会检查:

  • 共享参考
  • 所有remove AND add