How can I iterate through a list and use the values as labels or assigned names when instantiating a variable?

Zuckerbrenner :

If a user inputs a list of strings, I want to be able to iterate through that list and for each string in that list use that as a label for a variable. For example: User enters ["one", "three", "myVar"]. I want to instantiate a variable with name of "one", another one with the name of "three", etc. So I can have something like Integer one = 23; or String three = "hello"; How would I go about doing this in Java?

Tom Hawtin - tackline :

This would be pointless. To use the variables in the code you'd need to know what the user had entered. (Hacks like reflection aside.)

Almost certainly what you want is a Map keyed on the String entered. You still have the problem of the type of the maps value, which will depend upon exactly how you are going to use it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19077&siteId=1