我正在尝试编写一个递归程序,它可以打印从文件中获取的整数列表的所有排列,我使用 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);
}
}
最佳答案
当您修改正在迭代的集合时,会引发
java.util.ConcurrentModificationException
。在您的情况下,您在 permute
方法中迭代并修改 HashMap
。
如果没有抛出此异常,那么每次程序运行时都会得到奇怪且看似随机的结果,具体取决于从映射中添加或删除的整数生成的 hashCode
。
关于java - 使用 Hashmap 生成排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30790502/