开发者

Calling a variable by name, which isn't known while compiling

Is it possible to set a variable, if i want to have it flexible? I think an exmaple makes 开发者_如何学运维it easier to understand.

String hallo1;
String hallo2;

for(int i = 0; i < 2; i++) {
 hallo & i = Integer.toString(i);
}


No, you cannot. There's (fortunately) no such thing as eval() in Java.

Your best bet is to grab an Array or an ArrayList. Here's an Array example:

String[] hallos = new String[2];

for (int i = 0; i < 2; i++) {
    hallos[i] = Integer.toString(i);
}


It's technically possible with reflection, but I would advise against it unless you have a really good reason to do it. It would make your code much more difficult to debug and to read.

EDIT:
As the comments stressed:

  1. To clarify, I strongly suggest not to be tempted to use reflection. The other answers point out better ways to achieve your goal, such as an array.
  2. Reflection won't help you with local variables.


If you really have a need to have variables with names not known at compile time, you can achieve this effect by creating a structure that holds arbitrary names and values. Like, you could create a HashMap where the key is the name. Then your example above becomes something like:

  HashMap myData=new HashMap();
  for (int i=0;i<2;++i)
  {
    myData.put("hallo"+i,Integer.toString(i));
  }

Later you'd pull them out with:

  String whatever=myData.get("hallo1");

etc.

Of course you can't access values from the map as variables directly, you'd always have to do put and get to update and retrieve them, but the concept is the same.

That said, I'd be very cautious about doing this, because it makes the code difficult to maintain. If the names that you need are really coming out of the blue -- if you're writing generic code to read an arbitrary database table whose name was typed in by the user at runtime or something like that -- cool. But if you're thinking that this is a handy shortcut for something like:

  if (region.equals("1"))
    region1Total+=amount;
  else
    region2Total+=amount;

My simple answer would be DON'T!! The code is much easier to maintain if you use the IF statement and normal variables. Then anyone reading the code can look at your declarations and see what all the possible variables are. You can do text searches to find everywhere they're used. If you mis-spell a variable name, instead of magically creating a new variable you will get a clean compile-time error message. Etc.

On the other hand, if you COULD write something like

  String n=getInputFromScreen();
  String s=getAnotherInputFromScreen();
  hallo & n = s;

you would have no idea what variables exist in your program and no way to track where they are used.


You can use String array.
Something like:

 String[] hallos = new String[10];

 //when looping you can use regular (counter) loop or for each loop
 for (String s: hallos){
       //do whatever you want with each String
 }


No, the way to do something like this is with an array, list, or something similar. It's also more intuitive to group them together then to have many variables (that really should be related) floating around. There's no way to do this - in Java, at least. I've seen this done in scripting languages but that's a whole different thing...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜