Java Hashmap 值到数组
我有以下代码,它将一些数组添加到哈希图中,但随后我想访问这些数组以便稍后对它们进行一些工作。我已经走到这一步了,但无法弄清楚其余部分如何使其发挥作用......
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public static void main(String[] args) {
String[][] layer1 = {
{"to1", "TYPE1", "start"},
{"to2", "TYPE1", "start"}
};
String[][] layer2 = {
{"to3", "TYPE2" ,"item1"},
{"to3", "TYPE2" ,"item2"}
};
HashMap<String,Object> hashMap = new HashMap<String,Object>();
hashMap.put("layer1", layer1);
hashMap.put("layer2", layer2);
Iterator<Entry<String, Object>> iterator = hashMap.entrySet().iterator();
while(iterator.hasNext()){
hashMap.values().toArray();
for (???) {
// lets print array here for example
}
}
}
I have the following code which adds some arrays to a hashmap but then I want access those arrays to do some work on them later. I've gotten this far but can't figure the rest out to make it work....
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public static void main(String[] args) {
String[][] layer1 = {
{"to1", "TYPE1", "start"},
{"to2", "TYPE1", "start"}
};
String[][] layer2 = {
{"to3", "TYPE2" ,"item1"},
{"to3", "TYPE2" ,"item2"}
};
HashMap<String,Object> hashMap = new HashMap<String,Object>();
hashMap.put("layer1", layer1);
hashMap.put("layer2", layer2);
Iterator<Entry<String, Object>> iterator = hashMap.entrySet().iterator();
while(iterator.hasNext()){
hashMap.values().toArray();
for (???) {
// lets print array here for example
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来像家庭作业,但有一些建议 -
的形式 - 将其设为
,因为这就是您要存储的内容。你迭代了两次。你要么
- 迭代映射,无论是键、值还是条目。每个项目都是一个迭代器返回值,例如
hashmap.values.toArray 为您提供所有内容,这与迭代器正在执行的操作相同。
如果您只是迭代内容,那么您并没有真正使用映射,因为您从未利用过您的值可通过键获取的事实。
Smells like homework, but a few suggestions -
<String,Object>
- make it<String, String[][]>
, as that's what you're storing.You're iterating twice. You either
- iterate through the map, either the keys, values or entries. Each item is an iterator return value, e.g.
hashmap.values.toArray gives you all of the contents, which is the same thing your iterator is doing.
if you're only iterating through the contents, then you're not really using a map, as you're never making use of the fact that your values are available by key.
我可以建议你需要停止并花时间正确地学习Java。您的代码看起来像您正在尝试编写“perlish”...关联数组和数组而不是正确的类型。与使用 Java 思维方式设计和编写的代码相比,最终结果是 Java 代码缓慢且脆弱。
看起来你很有生产力,但你所生产的东西可能会在未来出现问题。
Can I suggest that you need to stop and take the time to learn Java properly. Your code looks like you are trying to write "perlish" ... associative arrays and arrays instead of proper types. The end result is Java code that is slow and fragile compared with code that is designed and written using the Java mindset.
It might seem like you are being productive, but what you are producing is likely to be problematic going forward.
你的 while 循环应该是这样的 -
Your while loop should look like -
您的代码在这里格式不正确:
您尝试使用迭代器“迭代器”进行迭代,但接下来您调用
要获取循环的下一项,您需要使用 iterator.next();来获取它。也可以将“对象”更改为 String[][] 或
List。或列表<列表<字符串>>
。您还可以使用“foreach”循环来简化使用“while”插入的代码:
或者如果您只需要值:
Your code is bad formed here:
You try to iterate using the Iterator "iterator" but next you call to
To get the next item of the loop you need to use iterator.next(); to fetch it. Also is good to change the "Object" by String[][] or to
List<String[]> or List<List<String>>
.Also you can use "for each" loop to simplify the code insted using the "while":
Or if you only need the values: