1

I have for example:

String invslot_ = "invslot_";
int i = 0;

Now I have a while(i < 44) loop and I want it every time it loops to "add" invslot_ and i. (Yes I could do invslot_ + i !)

But I want that infslot_ + 1 is recognized as a variable I have defined earlier!

I hope you understand what I mean, please answer if you have an idea.

5
  • 1
    I'm not sure what you mean? Commented Apr 27, 2015 at 15:52
  • You are looking for an Array Commented Apr 27, 2015 at 15:53
  • What about putting them into a data structure like Array or List? Commented Apr 27, 2015 at 15:53
  • 2
    Sounds like you want an array, a collection or a map. Commented Apr 27, 2015 at 15:53
  • 1
    No idea what you mean, but almost all "I want a String to reference a variable name" type questions have the same answer - use a Map. Commented Apr 27, 2015 at 15:54

1 Answer 1

0
Map<String, Number> vars = new HashMap<>();
vars.put("invslot_1", 3267);

for (int i = 0; i < 44; i++) 
    if (!vars.contains("invslot_" + i))     // put only if "var" is not already "assigned"
        vars.put("invslot_" + i, i);

System.out.println(vars.get("invslot_1"));  // 3267
System.out.println(vars.get("invslot_11")); // 11
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this it what i meant :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.