ConcurrentModificationException当我访问列表而不修改时

时间:2015-04-15 09:10:15

标签: java list multidimensional-array

当我使用此函数访问列表列表时,它会在第二个for循环中生成ConcurrentModificationException,但我不明白为什么会触发此异常。

 public static List<List<Dico>> weight_term(List<List<Dico>> sublists ,List<String> sinificativ )   
 {

    List<List<Dico>> matrix_node_term = new ArrayList<>();

    List<Dico> list_node =  new ArrayList<>();// a new list for node

    for (List<Dico> sublist : sublists) // to get each sublist List<Dico>
    {
        for (Dico dico : sublist)  // get each Dico in the sublist -->ConcurrentModificationException
        {
            String term =dico.getTerm();            
            int id = dico.getDocId();

            if(sinificativ.contains(term)) // if this term exist in sinificativ erm list
            {
                list_node.add(dico); // it add to list_node 
            }
            else
            {
                list_node.add(new Dico(id,term,0.0)); // it add to list_node with null weigth
            }
        }            
        matrix_node_term.add(list_node); // add each list to list of list 
    }

    return matrix_node_term;
 }

dico类用于存储该文档中的术语,文档ID和该术语的权重:

 public class Dico  implements Comparable 
 {
      private final String m_term;
      private double m_weight;
      private final int m_Id_doc;

      public Dico(int Id_Doc,String Term,double tf_ief ) 
      {
         this.m_Id_doc = Id_Doc;
         this.m_term = Term;
         this.m_weight = tf_ief;   
      }
}

触发此异常时不会对列表或其元素的sutucte进行任何修改。

1 个答案:

答案 0 :(得分:1)

问题来自用于在多个List中拆分List的函数:

List<List<Dico>> sublists = new ArrayList<>(change);

for (int i = 0; i < change; i++)
{ 
   sublists.add(list.subList(changes[i],changes[i + 1]));
}
     

解决方案来自pbabcdefp

List<List<Dico>> sublists = new ArrayList<>(change);
for (int i = 0; i < change; i++)
{
  sublists.add(newArrayList<Dico>(list.subList(changes[i], changes[i + 1])));
}

谢谢你的帮助