1

I'd like to automaticly create a new Variable for my Storage.

It should work like:

int var_count = getConfig().getInt("var_count");
++var_count;

int "var"+var_count = 123

If var_count is for example 4, it should generate a Variable named var4.

Blightbuster
  • 481
  • 7
  • 16
  • You need to use another language. – Neil Masson Jan 03 '16 at 22:53
  • Is there a certain thing that you're trying to do with this? It is possible that a structure such as a List or Map would be more suited to what you're trying to do. – Daniel Underwood Jan 03 '16 at 22:54
  • Lord, this has been asked and answered too many times on this site to count. Please consider searching before asking such common questions. – Hovercraft Full Of Eels Jan 03 '16 at 23:19
  • Please also look at [Assigning variables with dynamic names in Java](http://stackoverflow.com/q/6729605), [Create Dynamic Object with specific name in java](http://stackoverflow.com/q/19526359), [How to create new object name by user input](http://stackoverflow.com/q/20359064), [How to instantiate variable with name from a string?](http://stackoverflow.com/q/24005132), ... – Hovercraft Full Of Eels Jan 03 '16 at 23:26

1 Answers1

4

I'd like to automaticly create a new Variable for my Storage.

You can't. You could (however) make a Map<String, Integer> and store arbitrary key-value pairs. Something like

Map<String, Integer> map = new HashMap<>();
map.put("var" + var_count, 123);
System.out.println(map.get("var" + var_count));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249