如何访问哈希映射中字符串数组中的各个值?
我的映射声明如下:
Map<Integer, String[]> mapVar = new HashMap<Integer, String[]>();
我通过创建几个字符串数组并将它们与相应的整数放入我的映射中来初始化它。
然后我想迭代地图内字符串数组中的所有元素。 我尝试了这两种可能性,但它们没有给我正确的值:
for(int ii =0; ii < 2; ii++)
System.out.println(((HashMap<Integer, String[]>)mapVar).values().toArray()[ii].toString());
而且
mapVar.values().toString();
我也知道数组和整数将很好地进入地图,我只是不知道如何访问它们。
谢谢
My declaration of the map is as follows:
Map<Integer, String[]> mapVar = new HashMap<Integer, String[]>();
And I initialized it by making several string arrays and putting them into my map with a corresponding Integer.
I would like to then Iterate through all of the elements in my String array within the map.
I tried these two possiblities but they're not giving me the right values:
for(int ii =0; ii < 2; ii++)
System.out.println(((HashMap<Integer, String[]>)mapVar).values().toArray()[ii].toString());
and
mapVar.values().toString();
I also know the array and Integer are going into the map fine, I just don't know how to access them.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
Try
这将打印
Map
中所有数组中的所有Strings
。That will print all of the
Strings
in all of the arrays in theMap
.如果您希望能够将地图中的所有
String
值作为一个单元进行访问,而不是处理中间数组,我建议使用 番石榴 Multimap:当然,您还可以访问与特定键关联的
String
列表:If you want to be able to access all the
String
values in the map as one unit rather than dealing with the intermediate arrays, I'd suggest using a Guava Multimap:Of course you can also access the list of
String
s associated with a particular key: