使用迭代器-java.util.ConcurrentModificationException

时间:2018-12-12 15:39:39

标签: java iterator

所以,这使我返回

  

java.util.ConcurrentModificationException

并指向System.out.println行

Iterator<Autor> it = autores.iterator(); 
// Declaring a class iterator

public void listarAutores() {

    while (it.hasNext()) {
        String aux = it.next().getNomeCompleto(); 
        // Get string from Class Autor method
        System.out.println(aux);
        // Printing that string
    }
}

为什么会发生,我该如何解决?

2 个答案:

答案 0 :(得分:2)

如果您在两次调用方法之间修改了集合,就可能发生这种情况。例如:

_Connection con = com4j.typelibs.ado20.ClassFactory.createConnection();
try {
    con.provider("ADsDSOObject");
    con.open("AD-Anon-Search", "", "", -1/*default*/);

    _Command cmd = com4j.typelibs.ado20.ClassFactory.createCommand();
    try
    {
        cmd.activeConnection(con);
        cmd.commandText("<GC://" + this.domain + ">;" + filter.encode() + ";" + atributes + ";SubTree");
        _Recordset rs = cmd.execute(Variant.getMissing(), Variant.getMissing(), -1/*default*/);
        try
        {
            if (rs.eof())
                return nullValue;
            else
                return mapper.mapRecordSet(rs);
        } finally {
            rs.close();
            rs.dispose();
        }
    } finally {
        cmd.dispose();
    }
} finally {
    con.close();
    con.dispose();
    COM4J.cleanUp();
}

您应该在每次调用时在方法中创建一个新的迭代器,甚至更好的是,根本不使用迭代器:

listarAutores();
autores.add(anotherAuthor);
listarAutores();

答案 1 :(得分:1)

QRegularExpression GPSRegex("-|[0-9]+\\.+[0-9]+"); while (!file.atEnd()) { QString line = file.readLine(); i++; QRegularExpressionMatch matchGPSLat = GPSRegex.match(line); auto matchIterator = CoordinateRegex.globalMatch(line); if (matchGPSLat.hasMatch()) { QString GPSLatCoordinates = matchGPSLat.captured(0); while(matchIterator.hasNext()) { auto result = matchIterator.next(); if (!matchIterator.hasNext()) { GPSLongCoordinates = result.captured(0); } } double GPSlat = GPSLatCoordinates.toDouble(); double GPSLong = GPSLongCoordinates.toDouble(); } } 移至方法调用中。您可能在两者之间的某个地方修改了Iterator