'for' loop replaced by 'foreach'

My code is:

ArrayList<People> people = new ArrayList<>(); // people.add(...); // people.add(...); for (int i = 0; i < people.size(); i++) { if (people.get(i) > 60.0) System.out.println(people.get(i).toString()); } 

And I get the following warning:

'for' loop replaced by 'foreach'

How do I change a loop using foreach?

Thanks.

+6
source share
7 answers

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) { // Create and populate a list of people with individuals List<Person> people = new ArrayList<>(); people.add(new Person(32, "Fred")); people.add(new Person(45, "Ginger")); people.add(new Person(66, "Elsa")); // Iterate over the list (one person at a time) for (Person person : people) { if (person.age > 60) { System.out.println("Old person: " + person.name); } } } } 

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.

+12
source
 for(People objPeople : people){ //Loop code } 

The official documentation is here

+2
source
 people.get(i) retrun an object of class People. 

You cannot compare an object with a number operator in if.

EDIT

How to iterate Arraylist

 1) for (int i = 0; i < people.size(); i++) { if(people.get(i).getAvalue() > 60.0 ){//your code} } 

2)

 for(People p: people){ if(p.getAvalue()>60.0){//you code} } 
+2
source

You can replace this loop for each as shown below:

 ArrayList<People> peopleList = new ArrayList<>(); for(People people:peopleList) { Do something } 
+1
source

I find the for loop easier to understand later. Add this comment before the code to disable verification for this case only.

 //noinspection ForLoopReplaceableByForEach for (int i = 0; i < people.size(); i++) { Person person = people.get(i); ... } 
0
source

Of course, this is interchangeable, but I don’t understand what problem Lint sees with the β€œold school for the loop”. He works both ways.

This seems to be religious stuff:

"So, when should you use the for-each loop? Every time you can, it really decorates your code." ( https://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html )

0
source

As mentioned in other answers, a comparison should be made against some property of a person (age?).

Assuming you are fixing the comparison, the streaming version of the for loop might look like this:

 people.stream() .filter(p -> p.getAge() > 60.0) .forEach(p -> System.out.println(p.toString()); 
0
source

Source: https://habr.com/ru/post/972408/


All Articles