+ 1

[UNANSWERED ]Using value as variable

How can I use a string as a variable name? I want sth like that: String str = "var"; int (str) = 6;

14th Apr 2017, 5:04 PM
Alex Snaidars
Alex Snaidars - avatar
2 Answers
+ 2
The best way is using a java.util.HashMap like so: HashMap<String,Integer> variables = new HashMap<>(); String str = "var"; variables.put (str, 6); // set up variables.get(str); // get value
14th Apr 2017, 5:21 PM
merkrafter
14th Apr 2017, 5:23 PM
merkrafter