循环遍历包含 String 和另一个地图的地图
Loop through a map containing a String and another 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);
}
取决于您要打印的内容。
我这样声明我的地图:
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);
}
取决于您要打印的内容。