-3

Possible Duplicate:
Creating a variable name using a String value

Let's say that I want to do a for loop that creates 10 integers named num1, num2, num3....etc.

How can I do this? I cannot seem to find a way to use a predefined string as the name of an object.

Community
  • 1
  • 1
MagicGuy52
  • 571
  • 1
  • 6
  • 11
  • why not create an array of strings? – Mark Jan 25 '13 at 03:26
  • 2
    Why? Variable names aren't something that should generally be determined at *runtime*. And is this part of some kind of course or something? We seem to have been getting variations on this question lately... – T.J. Crowder Jan 25 '13 at 03:29
  • 1
    Probably the closest solution that's possible without going to unreasonable lengths is to use a `Map`. – aroth Jan 25 '13 at 03:32
  • 1
    The only way to do this is via bytecode manipulation, and it's generally not something you probably need or want to do. – Brian Roach Jan 25 '13 at 03:33
  • This is asked here at least once a week if not more frequently, and the answer is always the same. For instance: [Creating a variable name using a String value](http://stackoverflow.com/questions/8631935/creating-a-variable-name-using-a-string-value). Voting to close as duplicate. – Hovercraft Full Of Eels Jan 25 '13 at 03:59

1 Answers1

2

I don't think there is any way to do what you are asking. Based on what I think you are trying to accomplish, I think you should use an array or a list:

int [] num = new int[10];
for(int i = 0; i < 10; ++i){
   num[i] = // something
}


List<Integer> num = new ArrayList<Integer>();
for(int i = 0; i < 10; ++i){
   num.add(/* something */);
}
Gordon Bailey
  • 3,881
  • 20
  • 28
  • 1
    Regardless of *why* he wants to do what he's asking, this has nothing to do with his question. – Brian Roach Jan 25 '13 at 03:28
  • @BrianRoach: Oh, it probably has *something* to do with it. But yes, it's not an answer to the question actually asked. – T.J. Crowder Jan 25 '13 at 03:28
  • Fair enough, I've reworded to address the question that was actually asked. – Gordon Bailey Jan 25 '13 at 03:33
  • 2
    There are ways to do what he's asking. They are just extremely complex and completely impractical. – aroth Jan 25 '13 at 03:33
  • I guess this probably is the best way to do it. Before, I wanted to be able to have names to reference later, but it's not really that much of an inconvenience to just reference a spot in an array/list. Thanks. – MagicGuy52 Jan 25 '13 at 17:25