1

I have this:

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

Now I would like to change lbl1 to lbl+i.. so it prints out on lbl0,lbl1,lbl2,lbl3,lbl4.

How can I do this?

1
  • the lbl1.setText, i would like to change that to lbl[i].setText so it doesnt set to lbl1, but lbl0,lbl1,lbl2,etc Commented Dec 15, 2010 at 9:40

3 Answers 3

5

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

lblarray[i].setText(""+tarningar[i]);
Sign up to request clarification or add additional context in comments.

1 Comment

better yet, have only that array. There generally shouldn't be variables with index numbers in the name.
1

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]);

Comments

1

Store the labels in array and then:

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

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.