※ 引述《mikevada (vada)》之铭言:
: 我有几个字串像是下面:
: String s1="aaa";
: String s2="bbb";
: String s3="ccc";
: ........
: String sn="nnn";
: 现在我想用一个循环跑出所有的值
: for(int i=0; i<=n; i++)
: {
: out.println("s"+i);
: }
: 结果跑出来的变成字串s1、s2、s3,而不是变量s1,s2,s3
: 请问要怎么串起来才会输入成变量?谢谢大家。
...............Orz
String s1 = "aaa";
String s2 = "bbb";
String s3 = "ccc";
String s4 = "ddd";
Map<String, String> foo = new HashMap<>();
foo.put("s1", s1);
foo.put("s2", s2);
foo.put("s3", s3);
foo.put("s4", s4);
for (int i = 1; i <= 4; i++) {
out.println(foo.get("s" + i));
}
................XD
String s1 = "aaa";
String s2 = "bbb";
String s3 = "ccc";
String s4 = "ddd";
Function<String, String> foo = s -> {
switch (s) {
case "s1":
return s1;
case "s2":
return s2;
case "s3":
return s3;
case "s4":
return s4;
}
return "";
};
for (int i = 1; i <= 4; i++) {
out.println(foo.apply("s" + i));
}