How to browse lists?

2

I wanted a solution to go through a Practical List.

You do not have to show any results because I'm going to need to go through a list of people (objects) and generate an accounts receivable from each, but first I need to know how I can go through this list.

    
asked by anonymous 13.05.2016 / 13:39

3 answers

2

Assuming listaPessoas is a list of the object Pessoa .

So:

for(Pessoa pessoa : listaPessoas)
{
     pessoa.fazerAlgumaCoisa();
}

Or

for(int i = 0; i < listaPessoas.size(); i++)
{
    listaPessoas.get(i).fazerAlgumaCoisa();
}
    
13.05.2016 / 13:42
1

Only by completing the renan response, you do not need to use the list.stream () to use the forEach method, if the list you want to implement implements the Collection interface you can use the direct forEach example:

lista.forEach(elemento -> /* faz algo */);
    
01.07.2016 / 03:23
0

In addition to jbueno's responses, you can also use a iterator to browse a ArrayList.

It should be noted that foreach is a form of iterator's syntactic sugar. So both have the same result. Just curious.

    
19.05.2016 / 16:16