java - 组合两个变量来获取第三个的名称?

Noc*_*tis 3 java reflection variables

我正在尝试做这样的事情:

int x_1 = 1;
int x_2 = 2;

String s1 = "x";
String s2 = "_1";
Run Code Online (Sandbox Code Playgroud)

s1&s2将是来自用户或循环的变量,我希望能够int通过执行类似的操作来调用正确的变量(s1+s2).

可能吗?

lic*_*gwu 7

也许你可以用它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);
Run Code Online (Sandbox Code Playgroud)