通过嵌套的 for 循环初始化 hashmap
Initialising hash map via nested for-loop
我想要一个哈希映射来存储键和值的整数。通过使用嵌套的 for 循环,我认为它应该可以工作。我似乎没有完全理解嵌套循环中的程序流程。在我的代码下方:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Integer, Integer> hMap = new HashMap<>();
//initializing running variables here to reuse in "display hashmap" loop
int key = 48;
int values = 65;
//set keys and values to hashmap via nested for-loop
for (key = 48; key < 74; key++) {
for (values = 65; values < 91; values++) {
hMap.put(key, values);
}
}
//display hashmap via nested for-loop
for (key = 48; key < 74; key++) {
System.out.println("Key: " + key + ", Value: " + hMap.get(key));
}
}
}
这是当前控制台输出:
Key: 48, Value: 90
Key: 49, Value: 90
Key: 50, Value: 90
Key: 51, Value: 90
Key: 52, Value: 90
Key: 53, Value: 90
...
这是我想要的控制台输出:
Key: 48, Value: 65
Key: 49, Value: 66
Key: 50, Value: 67
Key: 51, Value: 68
Key: 52, Value: 69
Key: 53, Value: 70
...
我现在知道内部 for 循环一直运行到满足结束条件并因此覆盖变量 value
。但是如何才能达到如上所示的预期控制台输出?
您不需要嵌套循环。您正在尝试 put
映射中 48 到 73 之间的 key
和 65 到 90 之间的 value
的所有组合,但只保留每个键的最后一个值,因为地图不允许重复键。
一个循环即可满足您的需求:
int value = 65;
for (int key = 48; key < 74; key++) {
hMap.put (key, value++);
}
将 value++
放入 Map 会将 value
的当前值放入 Map 并为下一次迭代递增 value
变量。相当于:
int value = 65;
for (int key = 48; key < 74; key++) {
hMap.put (key, value);
value++
}
使用以下代码:
//set keys and values to hashmap via nested for-loop
int values = 65;
for (key = 48; key < 74 && values < 91; key++) {
hMap.put(key, values);
values++;
}
查看输出 here
Key: 48, Value: 65
Key: 49, Value: 66
Key: 50, Value: 67
Key: 51, Value: 68
Key: 52, Value: 69
Key: 53, Value: 70
Key: 54, Value: 71
.........
问题在循环中。对于每个键,您都在为所有值重复放置。
像这样添加和输出:
for (key = 48; key < 74; key++) {
for (values = 65; values < 91; values++) {
hMap.put(key, values);
System.out.println("put Key: " + key + ", Value: " + values);
}
}
你会看到你的错误:
put Key: 48, Value: 65
put Key: 48, Value: 66
put Key: 48, Value: 67
put Key: 48, Value: 68
put Key: 48, Value: 69
put Key: 48, Value: 70
put Key: 48, Value: 71
put Key: 48, Value: 72
put Key: 48, Value: 73
put Key: 48, Value: 74
put Key: 48, Value: 75
put Key: 48, Value: 76
put Key: 48, Value: 77
put Key: 48, Value: 78
put Key: 48, Value: 79
put Key: 48, Value: 80
put Key: 48, Value: 81
put Key: 48, Value: 82
put Key: 48, Value: 83
put Key: 48, Value: 84
put Key: 48, Value: 85
put Key: 48, Value: 86
put Key: 48, Value: 87
put Key: 48, Value: 88
put Key: 48, Value: 89
put Key: 48, Value: 90
put Key: 49, Value: 65
put Key: 49, Value: 66
put Key: 49, Value: 67
put Key: 49, Value: 68
put Key: 49, Value: 69
put Key: 49, Value: 70
...
映射中每个键的最后一个值始终是 90
试试这个:
for (key = 48; key < 74; key++) {
hMap.put(key, values++);
}
我想要一个哈希映射来存储键和值的整数。通过使用嵌套的 for 循环,我认为它应该可以工作。我似乎没有完全理解嵌套循环中的程序流程。在我的代码下方:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Integer, Integer> hMap = new HashMap<>();
//initializing running variables here to reuse in "display hashmap" loop
int key = 48;
int values = 65;
//set keys and values to hashmap via nested for-loop
for (key = 48; key < 74; key++) {
for (values = 65; values < 91; values++) {
hMap.put(key, values);
}
}
//display hashmap via nested for-loop
for (key = 48; key < 74; key++) {
System.out.println("Key: " + key + ", Value: " + hMap.get(key));
}
}
}
这是当前控制台输出:
Key: 48, Value: 90
Key: 49, Value: 90
Key: 50, Value: 90
Key: 51, Value: 90
Key: 52, Value: 90
Key: 53, Value: 90
...
这是我想要的控制台输出:
Key: 48, Value: 65
Key: 49, Value: 66
Key: 50, Value: 67
Key: 51, Value: 68
Key: 52, Value: 69
Key: 53, Value: 70
...
我现在知道内部 for 循环一直运行到满足结束条件并因此覆盖变量 value
。但是如何才能达到如上所示的预期控制台输出?
您不需要嵌套循环。您正在尝试 put
映射中 48 到 73 之间的 key
和 65 到 90 之间的 value
的所有组合,但只保留每个键的最后一个值,因为地图不允许重复键。
一个循环即可满足您的需求:
int value = 65;
for (int key = 48; key < 74; key++) {
hMap.put (key, value++);
}
将 value++
放入 Map 会将 value
的当前值放入 Map 并为下一次迭代递增 value
变量。相当于:
int value = 65;
for (int key = 48; key < 74; key++) {
hMap.put (key, value);
value++
}
使用以下代码:
//set keys and values to hashmap via nested for-loop
int values = 65;
for (key = 48; key < 74 && values < 91; key++) {
hMap.put(key, values);
values++;
}
查看输出 here
Key: 48, Value: 65
Key: 49, Value: 66
Key: 50, Value: 67
Key: 51, Value: 68
Key: 52, Value: 69
Key: 53, Value: 70
Key: 54, Value: 71
.........
问题在循环中。对于每个键,您都在为所有值重复放置。
像这样添加和输出:
for (key = 48; key < 74; key++) {
for (values = 65; values < 91; values++) {
hMap.put(key, values);
System.out.println("put Key: " + key + ", Value: " + values);
}
}
你会看到你的错误:
put Key: 48, Value: 65
put Key: 48, Value: 66
put Key: 48, Value: 67
put Key: 48, Value: 68
put Key: 48, Value: 69
put Key: 48, Value: 70
put Key: 48, Value: 71
put Key: 48, Value: 72
put Key: 48, Value: 73
put Key: 48, Value: 74
put Key: 48, Value: 75
put Key: 48, Value: 76
put Key: 48, Value: 77
put Key: 48, Value: 78
put Key: 48, Value: 79
put Key: 48, Value: 80
put Key: 48, Value: 81
put Key: 48, Value: 82
put Key: 48, Value: 83
put Key: 48, Value: 84
put Key: 48, Value: 85
put Key: 48, Value: 86
put Key: 48, Value: 87
put Key: 48, Value: 88
put Key: 48, Value: 89
put Key: 48, Value: 90
put Key: 49, Value: 65
put Key: 49, Value: 66
put Key: 49, Value: 67
put Key: 49, Value: 68
put Key: 49, Value: 69
put Key: 49, Value: 70
...
映射中每个键的最后一个值始终是 90
试试这个:
for (key = 48; key < 74; key++) {
hMap.put(key, values++);
}