Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加列表中包含的元素并将它们映射到字符串 输入_Java_Arraylist_Hashmap_Iterator_Concurrentmodification - Fatal编程技术网

Java 添加列表中包含的元素并将它们映射到字符串 输入

Java 添加列表中包含的元素并将它们映射到字符串 输入,java,arraylist,hashmap,iterator,concurrentmodification,Java,Arraylist,Hashmap,Iterator,Concurrentmodification,好名字好名字 OtherName OtherFine 其他人姓名其他人好 OtherName其他人好吗 其他人好吗 解释 我想制作一份列表,以便创建一份姓名列表以及对其施加的罚款总额 预期产量 我期望的输出(参考上面的示例)如下所示: [SomeName=SomeFine+OtherFine,OtherName=OtherFine+SomeOtherFine,SomeOtherName=SomeOtherFine] 代码 我尝试使用以下代码,但它给了我一个ConcurrentModificati

好名字好名字

OtherName OtherFine

其他人姓名其他人好

OtherName其他人好吗

其他人好吗

解释 我想制作一份列表,以便创建一份姓名列表以及对其施加的罚款总额

预期产量 我期望的输出(参考上面的示例)如下所示:

[SomeName=SomeFine+OtherFine,OtherName=OtherFine+SomeOtherFine,SomeOtherName=SomeOtherFine]

代码
我尝试使用以下代码,但它给了我一个
ConcurrentModificationException
。代码如下:

public List<Map<String, Integer>> calculateTotalFine(){

        List<Map<String, Integer>> myMapList = new ArrayList<Map<String, Integer>>();

        ListIterator<CrimeInfo> crimeIterator = list.listIterator();
        while(crimeIterator.hasNext()){
            String key = crimeIterator.next().getName();
            Integer value = crimeIterator.next().getFine();

            if(myMapList.isEmpty()){
                Map<String, Integer> aMap = new HashMap<String, Integer>();
                aMap.put(key, value);
                myMapList.add(aMap);
            }

            else{
                Iterator<Map<String, Integer>> mapIterator = myMapList.iterator();
                while(mapIterator.hasNext()){
                    if(mapIterator.next().containsKey(key)){  //<-- Line no. 29
                        Integer newFine = mapIterator.next().get(key) + value;
                        mapIterator.remove();

                        Map<String, Integer> nMap = new HashMap<String, Integer>();
                        nMap.put(key, newFine);
                        myMapList.add(nMap);
                    }
                    else{
                        Map<String, Integer> newMap = new HashMap<String, Integer>();
                        newMap.put(key, value);
                        myMapList.add(newMap);
                    }
                }
            }
        }
        return myMapList;
    }
公共列表calculateTotalFine(){
List myMapList=新建ArrayList();
ListIterator Crimeierator=list.ListIterator();
while(crimeIterator.hasNext()){
String key=crimeIterator.next().getName();
整数值=crimeIterator.next().getFine();
if(myMapList.isEmpty()){
Map aMap=newhashmap();
aMap.put(键、值);
myMapList.add(aMap);
}
否则{
迭代器mapIterator=myMapList.Iterator();
while(mapIterator.hasNext()){

如果(mapIterator.next().containsKey(key)){/问题是您正在迭代
myMapList
,但在迭代时对其进行了修改:

myMapList.add(newMap);
我还没有完全了解您要做什么,但从根本上说,您不应该在对集合进行迭代时将其添加到集合中。一种常见的方法是创建一个新集合,在迭代时对其进行修改,然后(如有必要)对原始集合进行批量修改


(正如提图斯所说,您在循环中也调用了两次
next()
。。您需要更加注意如何使用迭代器,并尽可能使用增强的for循环。)

问题是您正在迭代
myMapList
,但在迭代时修改它:

myMapList.add(newMap);
我还没有完全了解您要做什么,但从根本上说,您不应该在对集合进行迭代时将其添加到集合中。一种常见的方法是创建一个新集合,在迭代时对其进行修改,然后(如有必要)对原始集合进行批量修改


(正如提图斯所说,您还在循环中调用了两次
next()
。。您需要更加注意如何使用迭代器,并尽可能使用增强的for循环。)

ConcurrentModificationException
在迭代过快时会抛出,因为集合失败-fast@ifly那么我应该使用
java.util.concurrent
中的迭代器吗?@ifly6:No,那“太快了”这完全是假的。
ConcurrentModificationException
如果在对集合进行迭代时修改集合,则会引发该异常,无论该修改是在不同线程中执行还是在同一线程中执行。这与“迭代过快”无关。请为此提供一个,以便我们可以轻松复制。这里有很多错误,例如,您正在调用
next()
在同一个
迭代器上的同一次迭代中,您在迭代
集合时多次向该集合添加元素。
ConcurrentModificationException
在迭代过快时可能会引发,因为集合失败-fast@ifly那么,我应该使用
java.util.concurrent
中的迭代器吗?@ifly6:不,那“太快”完全是假的。如果在对集合进行迭代时修改集合,则会抛出
ConcurrentModificationException
,无论该修改是在不同线程还是在同一线程中执行。这与“迭代太快”无关。请为此提供一个示例,以便我们可以轻松地复制它。这里有很多错误,例如,您在同一个
迭代器上的同一次迭代中多次调用
next()
,在迭代过程中向
集合添加元素。