Java 在HashMap中存储ArrayList

Java 在HashMap中存储ArrayList,java,arraylist,hashmap,Java,Arraylist,Hashmap,我有以下代码。我想做的是使用置换函数填充ArrayList,将该数组保存在HashMap中,然后重新开始该过程(基本上,为每个键使用ArrayList填充HashMap)。我发布了下面的代码,但它不工作。我认为这是因为它存储了与我声明的列表相同的引用,而不是复制它。我是一名C scrub和Java新手,非常感谢您的帮助 public class Anagrams { public static HashMap<String, ArrayList<String>>

我有以下代码。我想做的是使用置换函数填充ArrayList,将该数组保存在HashMap中,然后重新开始该过程(基本上,为每个键使用ArrayList填充HashMap)。我发布了下面的代码,但它不工作。我认为这是因为它存储了与我声明的列表相同的引用,而不是复制它。我是一名C scrub和Java新手,非常感谢您的帮助

public class Anagrams 
{
    public static HashMap<String, ArrayList<String>> permutacii = new HashMap<String, ArrayList<String>>();
    public static ArrayList<String> tempList = new ArrayList<String>();


private static void permutation(String prefix, String str)
{
    int n = str.length();
    if (n == 0) 
        tempList.add(prefix);
    else 
    {
        for (int i = 0; i < n; i++)
            permutation(prefix + str.charAt(i), 
        str.substring(0, i) + str.substring(i+1));
    }
}

public static void main(String[] args) {
    findAll(System.in);
}

public static void findAll(InputStream inputStream) 
{
    Scanner scanner = new Scanner(inputStream);
    while(scanner.hasNextLine())
    {
        String line = scanner.nextLine();
        permutation("", line);
        permutacii.put(line, tempList);
        tempList.clear();
    }
}
}
公共类字谜
{
public static HashMap permutacii=new HashMap();
public static ArrayList templast=new ArrayList();
私有静态无效置换(字符串前缀、字符串str)
{
int n=str.length();
如果(n==0)
添加(前缀);
其他的
{
对于(int i=0;i
您只有一个列表,其中的多个引用存储在HashMap中。在每次迭代结束时清除该列表

解决问题的一种可能方法:

while(scanner.hasNextLine())
{
    String line = scanner.nextLine();
    tempList = new ArrayList<String>();
    permutation("", line);
    permutacii.put(line, tempList);
}
并相应地修改排列:

private static void permutation(String prefix, String str, ArrayList<String> tempList)
{
    int n = str.length();
    if (n == 0) 
        tempList.add(prefix);
    else 
    {
        for (int i = 0; i < n; i++)
            permutation(prefix + str.charAt(i),
                        str.substring(0, i) + str.substring(i+1),
                        tempList);
    }
}
私有静态无效置换(字符串前缀、字符串str、ArrayList模板)
{
int n=str.length();
如果(n==0)
添加(前缀);
其他的
{
对于(int i=0;i
您只有一个列表,其中的多个引用存储在HashMap中。在每次迭代结束时清除该列表

解决问题的一种可能方法:

while(scanner.hasNextLine())
{
    String line = scanner.nextLine();
    tempList = new ArrayList<String>();
    permutation("", line);
    permutacii.put(line, tempList);
}
并相应地修改排列:

private static void permutation(String prefix, String str, ArrayList<String> tempList)
{
    int n = str.length();
    if (n == 0) 
        tempList.add(prefix);
    else 
    {
        for (int i = 0; i < n; i++)
            permutation(prefix + str.charAt(i),
                        str.substring(0, i) + str.substring(i+1),
                        tempList);
    }
}
私有静态无效置换(字符串前缀、字符串str、ArrayList模板)
{
int n=str.length();
如果(n==0)
添加(前缀);
其他的
{
对于(int i=0;i
HashMap中的所有列表都是空的。每次在映射中放入一个列表后,都需要初始化一个新的
ArrayList
。HashMap中的所有列表都是空的。每次在映射中放入一个列表后,都需要初始化一个新的
ArrayList
。我试图将其作为参数添加到置换函数中,但我不确定如何编辑它,因为Java中没有指针。现在正在测试第一部分:)[]-我仍然得到空输出:|@Hydroxis您是否删除了
templast.clear()
语句?我测试了代码,它工作了。是的,只是做了,它工作了。现在需要调试它为什么不为输入中给定的某些字符串添加置换:/I我试图将其作为参数添加到置换函数中,但我不确定如何编辑它,因为Java中没有指针。现在正在测试第一部分:)[]-我仍然得到空输出:|@Hydroxis您是否删除了
templast.clear()
语句?我测试了代码,它工作了。是的,只是做了,它工作了。现在需要调试为什么不为输入中给定的某些字符串添加置换:/