Get the position in a for loop in java?

Asked

Viewed 259 times

-4

My java code:

for (Construtor usuario: users){

Construtor USER = new Construtor(user.getNomeUsuario);

arrayList.add(USER);

}

** How do I get the position of the loop in this configuration above? See that the old way is easier because:

for(int i = 0; i < users.size(); i++){

Toast......"Posição" + i.......Show();

}

** It’s easier the old-fashioned way, but I want to know how you do it the other way.

  • Please avoid long discussions in the comments; your talk was moved to the chat - As for the post, reduce the code to a [mcve] problem, to avoid further mismatches of information and enable reopening.

  • Don’t change your question, ask it properly the first time. When you change the question after you have an answer you invalidate this answer and it is not fair to the person who answered it. That is why it is important to ask the question properly right away, and that is why questions that are not good need to be closed as soon as possible, to avoid bad answers being given because the question is in bad shape.

2 answers

1

Hello yes there is a way to get position and value.

Take an example:

public class MyClass {
    public static void main(String args[]) {
        int index = 0;
        int[] abc = {2,5};
        for (int ho : abc) {
            System.out.println(ho+" is from index: "+index);
            index += 1;
        }
        index = undefined;
    }
}

Console:

2 is from index: 0
5 is from index: 1

And in the second method give value and position.

Take an example:

public class MyClass {
    public static void main(String args[]) {
        int[] abc = {2,5};
        for (int i = 0;i<abc.length;i++) {
            int value = abc[i];
            System.out.println(value+" is from index: "+i);
        }
    }
}

Console:

2 is from index: 0
5 is from index: 1
  • NOTE: This answer was from the first question before editing.
  • kkkkkk sorry to have made you take the trouble of answering.... I thank you very much for your explanation

  • See in the edition.... in my Mainactivity which had these loops "for".... I think the error was there.... but I saw that not...

  • Okay, fine. Which line of code has errors?

  • Well I don’t know..... The intention is to expand the listview showing the Childs, but when I click on the group nothing happens.... I looked for mistakes, but nothing I find.....

  • Which editor to use?

  • I did not understand this last question, but if you are talking about programming language is java and if the platform is Android Studio.....

  • If you want I can send you the . Apk for you take a look along with the source code....

  • Android Studio has console?

  • No need to order. You have already sent the code in the reply .

  • All in then..... but if you want to see the point apk I send you

Show 5 more comments

0

In Java, there is no native construction to get the Iterable index. Some languages allow this. For example, in PHP:

foreach ($items as $key => $value) { ... }

However, to do the same in Java, you will have to control the index for own account. Example:

int index = 0;

for(Construtor usuario: users) {
    System.out.println("Index atual: " + (index++));
}

Another alternative, as you put it, is to use the default form of for (without iteration of Iterable):

for(int i = 0; i < users.length; i++) {
    System.out.println("Index atual: " + i);
}
  • 1

    Thanks friend..... You really understood the question and gave a great answer.

Browser other questions tagged

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