开发者

Java: for loop using i

I have this:

for(int i=0;i<5;i++){
    lbl1.setText(""+tarningar[i]);

N开发者_如何学Goow I would like to change lbl1 to lbl+i.. so it prints out on lbl0,lbl1,lbl2,lbl3,lbl4.

How can I do this?


Add all of the labels to an array, then you can write:

lblarray[i].setText(""+tarningar[i]);


Instead of

Label lbl0;
Label lbl1;
Label lbl2;
Label lbl3;
Label lbl4;
/* ... */
for(int i=0;i<5;i++){
  lbl1.setText(""+tarningar[i]);

do this:

Label labels = new Label[5];
/* ... */
for(int i=0;i<5;i++){
  labels[i].setText(""+tarningar[i]);


Store the labels in array and then:

for(int i=0;i<5;i++){
    labelArray[i].setText(""+tarningar[i]);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜