我正在尝试做这样的事情:
int x_1 = 1;
int x_2 = 2;
String s1 = "x";
String s2 = "_1";
s1
& s2
将是来自用户或循环的变量,我希望能够调用正确的 int
变量做类似 (s1+s2)
的事情。
这可能吗?
最佳答案
也许你可以使用 Map
来做到这一点。
Map<String,Integer> map = new HashMap<>();
map.put("x_1",1);
map.put("x_2",2);
String s1 = "x";
String s2 = "_1";
map.get(s1+s2);
关于java - 组合两个变量以获得第三个变量的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593878/