【问题标题】:HashMap<String, ArrayList<String>>HashMap<String, ArrayList<String>>
【发布时间】:2014-11-14 09:05:33
【问题描述】:

我正在尝试让 HashMap 存储一个字符串和一个 ArrayList&lt;String&gt;. 基本上我想要的是第一个字符串是一个名称,数组列表包含用户去过的每个国家。

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 也没有按照我想要的方式存储键/值吗?

【问题讨论】:

  • 您需要提供其余代码。

标签: java hashmap


【解决方案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 从用户那里获取姓名和国家/地区。
【解决方案2】:

通常您的问题是由于对所有人重复使用相同的 ArrayList,因此所有人共享相同的国家/地区,全部由一个 ArrayList 持有。一种解决方案是为每个人创建一个新的 ArrayList——这意味着 new ArrayList&lt;String&gt; 需要在某个循环中,您可以在其中创建旅行者的姓名字符串。

例如,您可以将代码更改为:

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.");
    }

【讨论】:

  • 谢谢,我会试试代码,看看它把我带到哪里:)
  • 这两行代码是做什么的? "archive = = ArrayList(); // **********", "arkivet.put(name, archive); // ************"因为在这些行上运行程序时,我只是得到了非法的表达式开始
  • @Hablo:错字。发生这种情况是因为我正在使用无法编译的代码 sn-ps,因此我无法让编译器通知我何时我的胖手指向上。
  • @Hablo:但无论如何,不​​要复制我的代码,复制我的想法。重新初始化 ArrayList,该 ArrayList 在您创建旅行者的循环内保存国家名称。
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
相关资源
最近更新 更多