开发者

Java Can Variables Be Used in Variable Names

开发者 https://www.devze.com 2023-03-05 02:41 出处:网络
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.

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 pos开发者_如何学Csible in Java?


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
}


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)

0

精彩评论

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