我正在尝试编写一个递归程序,可以打印从文件中取出的整数列表的所有排列,我使用HashMap存储列表,其中键是数字,值是键的次数发生在列表中。代码如下:
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
Set<Integer> keys = map.keySet();
for(int num: keys)
{
System.out.print(Integer.toString(num)+",");
int val = map.get(num) -1;
if(val == 0)
{
map.remove(num);
}
else
{
map.put(num, val);
}
permute(map);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}
我收到并发错误,但我不明白为什么,错误如下:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at perm.permute(perm.java:15)
at perm.permute(perm.java:27)
at perm.permute(perm.java:27)
at perm.permute(perm.java:27)
at perm.main(perm.java:48)
--------------------基于评论编辑的代码--------------------
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
Iterator it = map.entrySet().iterator();
//Set<Integer> keys = map.keySet();
while(it.hasNext())
//for(int num: keys)
{
Map.Entry pair = (Map.Entry)it.next();
int num = (int) pair.getKey();
System.out.print(Integer.toString(num)+",");
int val = (int) pair.getValue()-1;
//HashMap<Integer, Integer> newMap = map;
//int val = map.get(num) -1;
if(val == 0)
{
//newMap.remove(num);
it.remove();
}
else
{
//newMap.put(num, val);
pair.setValue(val);
}
permute(map);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}
同样的错误仍然存在。然后我回到初始方法并存储了我试图迭代的HashMap的临时副本,并且仅修改了临时副本而不是原始副本,同样的错误仍然存在。
----基于答案1的代码编辑------
import java.util.*;
import java.io.*;
public class perm
{
public static void permute(HashMap<Integer, Integer> map)
{
if(map.isEmpty())
{
System.out.println();
}
else
{
//Iterator it = map.entrySet().iterator();
HashMap<Integer, Integer> newMap = map;
Set<Integer> keys = map.keySet();
//while(it.hasNext())
for(int num: keys)
{
//Map.Entry pair = (Map.Entry)it.next();
//int num = (int) pair.getKey();
System.out.print(Integer.toString(num)+",");
//int val = (int) pair.getValue()-1;
//HashMap<Integer, Integer> newMap = map;
int val = map.get(num) -1;
if(val == 0)
{
newMap.remove(num);
//it.remove();
}
else
{
newMap.put(num, val);
//pair.setValue(val);
}
permute(newMap);
}
}
}
public static void main(String args []) throws FileNotFoundException
{
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
Scanner in = new Scanner(new File(args[0]));
int num;
while(in.hasNextInt())
{
num = in.nextInt();
if(map.containsKey(num))
{
map.put(num, map.get(num)+1);
}
else
{
map.put(num,1);
}
}
permute(map);
}
}
答案 0 :(得分:2)
java.util.ConcurrentModificationException
。在您的情况下,您正在迭代并修改HashMap
方法中的permute
。
如果没有抛出此异常,那么每次运行程序时,最终都会出现奇怪且看似随机的结果,具体取决于从地图中添加或删除的整数的生成hashCode
。
答案 1 :(得分:0)
您正在获取ConcurrentModificationException,因为您正在尝试在迭代时修改map。理解它的好链接 http://javarevisited.blogspot.com/2012/02/fail-safe-vs-fail-fast-iterator-in-java.html