A list called people usually contains Person objects.
Here is a sample code that shows how to use the for-each loop:
public class Demo { private static class Person { public int age; public String name; public Person(int age, String name) { this.age = age; this.name = name; } } public static void main(String... args) {
You can also read the Oracle Java documentation about each cycle .
General form:
for (Person person : people) { ... }
Instead:
for (int i = 0; i < people.size(); i++) { Person person = people.get(i); ... }
Recommended for each of them, because he suffers. However, if you need to know the index number, you will need to use the original for the loop or increment the counter inside each for each.
source share