+ 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;
2 Réponses
+ 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
- 1
Visit https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html for further information.