HashMap<String, ArrayList<String>>

3

我想让HashMap存储一个字符串和一个ArrayList<String>。基本上,我想让第一个字符串是名称,并且数组列表包含用户去过的每个国家。

Example:
{Andrew, [USA, China]}
{Lola, [Sweden, Denmark]}

我遇到的问题是,每当用户输入一个国家时,该国家将以两种名称存储:
"The HashMap contains: {Andrew=[USA, China, Sweden, Denmark], Lola=[USA, China, Sweden, Denmark]}"

ArrayList<String> namelist = new ArrayList<>();
ArrayList<String> test = new ArrayList<>();
ArrayList<String> archive = new ArrayList<>();
HashMap<String, ArrayList<String>> thearchive = new HashMap<>();

(input.equals("New Country")){
    System.out.println("Name of the Person? ");
    String name = in.nextLine();
    if(namelist.contains(name)){
        System.out.println("Which Country have you visited?");
        String country = in.nextLine();
        if (archive.contains(country)){
            System.out.println("You've already visited this country.");
        }
        else {
            test.add(country);
            thearchive.put(name, country);
            System.out.println("HashMap Contains: " + thearchive);
        }
    }
    else{
        System.out.println("The person does not exist please add first.");
    }

有人知道为什么HashMap不能按照我想要的方式存储键/值对吗?

1
你需要提供你代码的其余部分。 - aarti
2个回答

1
通常您的问题是由于所有人都重复使用同一个ArrayList导致的,因此所有人都共享相同的国家,这些国家都由一个单一的ArrayList持有。解决方案是为每个人创建一个新的ArrayList -- 这意味着new ArrayList<String>需要在创建旅行者姓名字符串的某个循环内部。
例如,您可以将代码更改为以下内容:
ArrayList<String> namelist = new ArrayList<>();
ArrayList<String> test = new ArrayList<>();

// ArrayList<String> archive = new ArrayList<>();

HashMap<String, ArrayList<String>> arkivet = new HashMap<>();

(input.equals("New Country")){
    System.out.println("Name of the Person? ");
    String name = in.nextLine();

    archive = ArrayList<String>(); // **********
    arkivet.put(name, archive);  // ************

    if(namelist.contains(name)){
        System.out.println("Which Country have you visited?");
        String country = in.nextLine();
        if (archive.contains(country)){
            System.out.println("You've already visited this country.");
        }
        else {
            test.add(country);
            thearchive.put(name, country);
            System.out.println("HashMap Contains: " + thearchive);
        }
    }
    else{
        System.out.println("The person does not exist please add first.");
    }

谢谢,我会尝试这段代码,看看能带我到哪里 :) - Hablo
这两行代码是做什么的?“archive = = ArrayList<String>(); // **********”,“arkivet.put(name, archive); // ************”因为当我运行这些代码时,程序会提示非法表达式的错误。 - Hablo
@Hablo:打错字了。这是因为我正在使用无法编译的代码片段,所以我无法让编译器在我手指打错时通知我。 - Hovercraft Full Of Eels
@Hablo:无论如何,不要抄袭我的代码,抄袭我的思路。在创建旅行者的循环中重新初始化保存国家名称的ArrayList。 - Hovercraft Full Of Eels

1

你的代码还没有完成,所以我无法知道测试和归档是什么。但是让我们创建一个示例:

HashMap<String, ArrayList<String>> list = new HashMap<String, ArrayList<String>>();
ArrayList<String> anotherlist = new ArrayList<String>();
anotherlist.add("Brazil");
anotherlist.add("USA");
list.put("Augusto", anotherlist);

我希望它能帮到你。


问题在于我需要通过 System.in 从用户那里获取姓名和国家信息。 - Hablo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接