gpt4 book ai didi

java - 解析 HashMap 中的字符串数组

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

所以基本上,我试图解析 HashMap 的每个映射条目,同时读取字符串数组中的每个字符串值。然后,我将使用数组中的每个字符串值作为特定方法的参数。现在,我知道如何解析每个映射条目,只是迭代映射中的字符串数组让我感到困惑。有什么建议吗?

我的代码:

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

Set set = roomSite.entrySet();

Iterator<Map.Entry<String, String[]>> iterator = set.iterator();

while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(Arrays.deepToString((String[]) mentry.getValue()));
}

输出:

key is: 0 & Value is: [wall, d0, wall, wall]
key is: 1 & Value is: [d0, wall, d1, wall]
key is: 2 & Value is: [wall, wall, wall, d1]

最佳答案

您已经有了字符串数组,只需将其分配给一个变量即可:

String[] stringArray = (String[]) mentry.getValue();

for (String str : stringArray){
// process str
}

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

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