The problem with the loops. Do I need to access 10 tags through a loop in java?

I have a problem with loops. I need to access 10 labels that have names like label1, label2, label3 .... etc. I need to know if I can access these shortcuts after going through a loop in java?

+4
source share
6 answers

How about using List or array

 List<JLabel> labels = new ArrayList<JLabel>(); labels.get(index); 
+5
source

Change these labels as an array and access it with an index.

For instance:

 JLabel[] labels = new JLabel[10]; for (int i = 0; i < labels.length; ++i) { labels[i] = new JLabel("Label " + i); } for (int i = 0; i < labels.length; ++i) { // access each label. } 
+4
source

Put your tags in a LinkList or array Then you can access these arrays or linkList in a loop

+4
source

If you cannot change the names of the shortcuts / put them in an array, you can create an array of shortcuts to tags and fill it at the beginning of your program with a list of your tags.

+2
source

"Shortcut access" is fuzzy. Do you mean different instances of java.awt.label? If so, you can simply iterate over them, when they are in the list, for each operator.

0
source

If you are talking about Java shortcuts, you can use the switch statement instead. If you are talking about objects like JLabel, use an array or ArrayList.

0
source

All Articles