How to get an advanced "for" score?

Asked

Viewed 467 times

1

I was wondering if I could get some kind of control variable, from a for "advanced".

For example:

In a be normal, I do it this way:

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

this variable i I can use to go through the components, example:

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

in a be advanced I would do:

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

How would I get the position that the for is and traverse all components?

Obs: MeuComp is an interface that contains methods, which are implemented by components such as JTexefield, JComboBox and etc. componentes is a List of these components, but I believe that was not so important, the example may be on top of anything.

3 answers

5


It doesn’t. The second example is what’s called foreach and its function is to take each element of a data collection and use it directly. If you want an index use the for simple of the first example, so it is possible to go through all elements and have an index variable. So there are both.

2

Complementing what @Maniero said, even if you want to get the index, you can do

int index = componentes.indexOf(comp);

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

  • Thank you all !

  • 1

    How do you know componentes has a indexOf()? Even if you do, you know the cost of it?

  • 1

    I don’t know, since the example is simple, I just gave him a tip in case he needs it. In this case the cost does not matter, because the knowledge that it is possible is worth more.

  • @bigown by what I understood of the question(and by what I already answered of this OP), componentes is a List.

  • 1

    @Articuno It may be, but it does not have in the question, but seeing what cost (exponential, by the way) does not matter gave that dry swallowed.

  • @bigown I don’t know why all the fuss, this question is more like some academic activity or something. If it was for any activity that performance really mattered, he wouldn’t be asking that.

  • 1

    @Marcusdacorréggio is that in my conception the right is always the right and always desirable, teach the right to those who are starting is the best to do. I think it’s a shame that you think teaching anyway is ideal.

  • @bigown at no time did I say that was ideal. I just showed him that there is possibility. I agree with you, we should always teach the right thing. But to recognize right, we also need to be aware of wrong, even if not so wrong...

  • 1

    Nesse caso o custo não interessa, pois o conhecimento de que é possível **vale mais** And essa pergunta tá mais pra alguma atividade acadêmica this leaves implicit that for being academic can do anyway, when it should be the opposite, since it is a time to learn the right.

  • @bigown you who interpreted something implicit, I had no intention. The academic environment must learn right and wrong. Each with his own opinion, the important thing is that he had his answer. Hug.

Show 5 more comments

1

If you want a functional alternative (Java 8+), yes is also possible. On the Baeldung website you have several examples.

What I came to use was the protopack. So I’m gonna focus on him.

The first thing you need is to import the dependency. If you are using Maven:

<dependency>
    <groupId>com.codepoetics</groupId>
    <artifactId>protonpack</artifactId>
    <version>1.13</version>
</dependency>

Or else Gradle:

implementation com.codepoetics:protonpack:1.13

Or, if not using package manager, put the jar in the classpath (choose a version here).

To use the element with its index, just do the following:

Collection<T> myCollection;

    ...

Stream<Indexed<T>> collectionWithIndexes = StreamUtils
      .zipWithIndex(myCollection.stream());

One of the advantages with the StreamUtils is that you have a Stream from Java 8 to manipulate. The object Indexed<T> has 2 methods of interest: getIndex() and getValue().

You haven’t described how you want to manipulate objects, so I can play any game here. For example, print the content and the hashCode of the elements:

StreamUtils.zipWithIndex(componentes)
  .map(indexedComp  -> indexedComp.getIndex() + ", hash " + indexedComp.getValue().hashCode())
  .forEach(System.out::println);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.