3

I've created 3 variables

radio1 radio2 radio3

is it possible to use a for loop and from a String called "radio" to add the counter in the end in order to get the variable?

for instance something like this

for(i=1;i<=3;i++)
    if(("radio" + i).method())
          do something

thanks in advance

4
  • 4
    No, check out arrays. Reflection is technically an answer to this... just like "build a language with stdlib that does it that way" is technically an answer to "function X does Y, I want it to do Z". Commented Sep 24, 2011 at 19:29
  • 3
    In context to his question, Reflection is not an answer. Commented Sep 24, 2011 at 19:40
  • 1
    @Saket: agrees with Johan: reflection is not the solution to the overall problem and is adding additional danger without benefit when simple arrays or hashmaps would solve this easily. Commented Sep 24, 2011 at 19:55
  • 1
    In Java what's important is not so much the variable name, which is almost non-existent when the code is running, but rather having a reference to your objects. An array or map will do this nicely. Commented Sep 24, 2011 at 20:16

4 Answers 4

7

You can use a Radio object and use arrays instead:

Radio[] radios = new Radio[] {radio1, radio2, radio3};
for(i=0;i<3;i++)
    if(radios[i].method())
          do something

If you want to access variable by forming their names, you can also use Java's reflection API. But it is an expensive operation and is not advisable in general.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks to me like you want to use a Dictionary or similar data structure, which lets you store objects indexed by, for example, a string.

EDIT

As several people noted, HashMap is a more modern and better alternative.

5 Comments

Please note that Dictionary and Hashtable are deprected for quite some time. Please do NOT recommend these for new code or for new coders.
Agreed - Map implementations should be used instead.
@A.H. While I would recommend HashMap myself, Hashtable is not deprecated and has been retrofitted with generics. (Dictionary is obsolete ;-)
Hashtable is not @deprecated but it is 'deprecated'. The forced synchronization overhead, some duplicate methods and the good replacement HashMap justify this more than enough. Also note, that both classes has been retrofitted to generics, therefore this is not the only one criteria for deprecation without @deprecated.
Sorry, C# on the brain as of late! Let me correct the entry.
0

You should be using an array so you can easily iterate over all of them.

You could also play with Lists if you do not know how many items there wil be.

Comments

0

The most convenient way is to use an intermediate array like this:

Radio radio1, radio2, radio3;
for(Radio radio: Arrays.asList(radio1, radio2, radio3))
    if( radio.method() )
        doSomething();

The java.util.Arrays is provided by the JDK.

This works. But please think about it, how many times you really want three separate Radio instances vs. how many times you are not interested in one of them but in all of them. In the later case put the three instances into a Collection or into an array right from the start and forget about the three individual instances.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.