Accessing the value of a variable as a string in Java
•
Java
I have a string containing the name of the variable I want to get the value of this variable
int temp = 10; String temp_name = "temp";
Can temp be used_ Name access value 10?
Solution
I suggest you use map < string, integer > instead:
Create a map by executing
Map<String,Integer> values = new HashMap<String,Integer>();
Then change
int temp = 10;
to
values.put("temp",10);
And use to access the value
int tempVal = values.get(temp_name);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码