我像这样声明我的 map :
Map<Integer, Map<Integer, Integer>> junctions = new HashMap<>();
并用数据填充它:
for (int i = 0; i < N; i++) {
String[] coordinates = s.nextLine().split(" ");
junctions.put(i, new HashMap<Integer, Integer>());
junctions.get(i).put(Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]));
}
但我无法打印它,或使用它的内容。
我已经尝试过这样的:
for (Map<Integer, Map<Integer, Integer>> m : junctions.entrySet()) {
System.out.println(m.getKey() + "/" + m.getValue());
}
我也尝试过使用junctions.values()
而不是junctions.entrySet()
我需要做什么?
最佳答案
应该是
for (Map.Entry<Integer, Map<Integer, Integer>> e : junctions.entrySet()) {
System.out.println(e.getKey() + "/" + e.getValue());
}
或
for (Map<Integer, Integer> m : junctions.values()) {
System.out.println(m);
}
取决于您要打印的内容。
关于java - 循环遍历包含字符串和另一个映射的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29545406/