How to get index of an advanced "for"?

2

I was wondering if I can get some kind of control variable from an "% advanced"%. "

For example:

On a normal for, I do this as follows:

 for (int i = 0; i < algumaCoisa.size() ou length; i++){
     //código..
}

this variable for I can use to go through the components, eg:

((JComponent) componentes.get(i)).requestFocus();

in an advanced for I would:

for(MeuComp comp: componentes){
//código..
}

How do I get the position that i is and go through all components?

Note: for is an interface that contains methods, which are implemented by components such as MeuComp , JTexefield , and so on. JComboBox is a componentes of these components, but I think it would not be that important, the example could be on top of anything.

    
asked by anonymous 27.07.2017 / 03:12

2 answers

4

It does not. The second example is foreach and its function is to take each element of a data collection and use it directly. If you want an index, use the% simple% of the first example, so you can go through all elements and have an index variable. So there is both.

    
27.07.2017 / 03:40
1

Complementing what @bigown said, even if you want to grab the index, you can do

int index = componentes.indexOf(comp);

This way you get the index of your element. However, if I am not mistaken, it is necessary that its MeuComp class has equals and hashCode implemented

    
27.07.2017 / 21:01