【问题标题】:String Array Values and HashMaps字符串数组值和 HashMap
【发布时间】:2015-11-09 21:49:25
【问题描述】:

我一直在尝试制作迷宫类型的游戏,方法是使用 HashMaps 从配置文件中读取来区分房间属性和门属性。如果我只打印 String[] 值,它们会打印相应的配置值,但是,当我尝试使用 HashMap 通过键调用它来打印值时,它会告诉我这些值为空。 有什么建议吗?

我的代码:

     while (file.hasNextLine()) {
            String line = file.nextLine();
            if (line.isEmpty()) {
              continue;
            }
            Scanner scanner = new Scanner(line);
            String type = scanner.hasNext() ? scanner.next() : "";

            String name = scanner.hasNext() ? scanner.next() : "";

            String wall1 = scanner.hasNext() ? scanner.next() : "";

            String wall2 = scanner.hasNext() ? scanner.next() : "";

            String wall3 = scanner.hasNext() ? scanner.next() : "";

            String wall4 = scanner.hasNext() ? scanner.next() : "";

            HashMap<String,String[]> roomSite = new HashMap<String, String[]>();
            HashMap<String,String[]> doorSite = new HashMap<String, String[]>();

            String[] options = new String[]{wall1,wall2,wall3,wall4};

            if(type.equals("room")){
                roomSite.put(name, options);
            }else if(type.equals("door")){
                doorSite.put(name, options);
            }else{
                System.out.println("Error in config file. Please check that all options are valid.");
            }
            System.out.println(roomSite.get(0));
            System.out.println(doorSite.get("d0"));
  }

输出:

null
null
null
null
null
null
null
[Ljava.lang.String;@e9e54c2
null
null

config.ini

room 0 wall d0 wall wall
room 1 d0 wall d1 wall
room 2 wall wall wall d1
door d0 0 1 close
door d1 1 2 open

【问题讨论】:

  • 好吧,当它打印null 时,您是否将0 作为roomSite 中的键或"d0" 作为doorSite 中的键?
  • 如果您在线路上使用String.split() 而不是所有那些扫描仪,可能会更有意义。

标签: java arrays string hashmap


【解决方案1】:

你有System.out.println(roomSite.get(0));,而它应该是System.out.println(roomSite.get("0"));

您的地图从String 映射到String[],而不是IntegerString[]

另外,如果你想得到更有意义的打印语句而不是[Ljava.lang.String;@e9e54c2,你可以使用Arrays.toString() 方法。

【讨论】:

  • 您好,在将键设为“0”后,现在它给了我:[Ljava.lang.String;@e9e54c2 后跟空值,这正常吗?
  • 您应该在开始读取文件之前初始化地图。每次阅读一行时,您都在创建一个新的HashMap。因此,在第一次迭代中,当您在房间 0 中阅读时,您将其详细信息添加到地图中,这就是您看到 [Ljava.lang.String;@e9e54c2 打印的原因。之后,您将创建新地图并添加其他房间/门。 “0”细节不在新地图中,因此您只能在后续迭代中看到null
  • 哦,好的。现在可以了。非常感谢您的帮助!我现在被困了大约 2 小时 :)
  • 没问题,很高兴为您提供帮助!
【解决方案2】:

在遍历您的文件之前初始化您的HashMaps。试试这样的:

public static void main(String []argh) throws FileNotFoundException
{
    Scanner scan = new Scanner(new File("config.ini"));
    HashMap<String, String[]> roomSite = new HashMap<>();
    HashMap<String, String[]> doorSite = new HashMap<>();
    while(scan.hasNext()){
        String type = scan.next();
        String name = scan.next();
        String[] walls = scan.nextLine().trim().split(" ");
        switch(type){
        case "room":
            roomSite.put(name, walls);
            break;
        case "door":
            doorSite.put(name,  walls);
            break;
        default:
            System.out.println("Error in config file. Please check that all options are valid.");
        }
    }
    System.out.println(Arrays.toString(roomSite.get("0")));
    System.out.println(Arrays.toString(doorSite.get("d0")));
}

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 2016-01-14
    • 2015-11-03
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多