gpt4 book ai didi

java - 字符串数组值和 HashMap

转载 作者:行者123 更新时间:2023-12-02 04:12:55 25 4
gpt4 key购买 nike

我一直在尝试通过使用 HashMap 从配置文件中读取来区分房间属性与门属性来制作迷宫类型的游戏。如果我只是打印 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

配置.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

最佳答案

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

您的 map 从 String 映射到 String[],而不是从 Integer 映射到 String[]

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

关于java - 字符串数组值和 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com