• Post Reply Bookmark Topic Watch Topic
  • New Topic

Make variable names from a String array  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to write code so that variable names can be derived from a String array?

In a very simplified form, something like this, although this specifically doesn't work.



A nudge in the right direction would be appreciated. Thanks.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do that, but there is a collection called a Map (see java.util.Map) which can be used to translate a Key (the string) to a value (the number). But you should sit back and think about your design. This sort of question usually indicates a problem with the design, and you might be better off sitting back and considering what you need to do (see WhatNotHow). For example, what is your data, what will you be doing with it, how do you get it, would you be better off using a Class to represent your data? etc...
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but you ain't gonna like it:-I can't think of any other way to do it. The variables no longer have names at runtime, so you cannot access their names to change or anything like that.

I am not sure whether you can even do that sort of thing by reflection.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!