1

I have this little script repeated below in my code a few times. I know that I could run a function to do this easily, but can I use a variable as a variable name like in PHP.

   if (4val != null && 4val.length() > 0){
            Button 4 = new Button(this);
            4.setText(4val);
            4.setTextSize(20);
        }

I want to be able to do something like

i=1;
while{i > 10}{
    $$i = value;
    //do stuff with $$i
    i++;
}

Is this possible in Java?

1
  • Is your intention to have access to those variables after the loop is done? And why wouldn't you use some sort of collection construct? Commented May 10, 2011 at 17:50

2 Answers 2

5

No. But you can stick the buttons in an array, and then iterate through.

Button[] buttons = new Button[10];
// instantiate all the buttons
for (int i = 0; i < buttons.length; i++) {
   // update the button
}
Sign up to request clarification or add additional context in comments.

Comments

3

use Map instead

map.put("key","val");    
map.get("key");

Well you can also use array, List , but if you use HashMap you retrieval process would be almost o(1)

1 Comment

The retrieval process will be almost O(1). As opposed to an actual array, when it will be actually O(1)?

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.