java如何设置动态变量,如何在Java中动态创建变量?
I need to create new variables Strings such that
String person1 = "female";
String person2 = "female";
........
........
String person60 = "male";
........
String person100 = "male";
java系统变量设置
This is what I tried
for (int i = 1; i <101; i++) {
if (i<60) {
String person+i = "female";
}
else {
String person+i = "male";
}
}
Can anybody help me correct this code?
解决⽅案
A Map allows you to relate any key with any value. In this case, the key is the name of the variable, and the value is the value Map details = new HashMap<>();
for (int i = 1; i <101; i++) {
if (i<60) {
details.put("person" + i, "female");
}
else {
details.put("person" + i, "male");
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。