Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How to automate creating objects in a loop? Why doesn't this code work?
String strLbl[] = {"Model","Weight","Length","Age","Number of keys"}; JLabel lbl[] = new JLabel[5]; for (int i=0;i<strLbl.length;i++){ JLabel lbl[i] = new JLabel(strLbl[i]); }
The type declaration isnt allowed for an array element assignment:
JLabel lbl[i] = new JLabel(strLbl[i]);
should be
lbl[i] = new JLabel(strLbl[i]);
Add a comment
As a side note, in Java 8:
JLabel lbl[] = Arrays.stream(strLbl).map(s -> new JLabel(s)).toArray();
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.