use while or foreach for array management?

1

I would like to know what are the advantages and disadvantages, such as the situations to use the while and the foreach working with fixes

I have worked with both but I do not know how they differ, if I have noticed is that when you get the values if you use within this two facts, the first load variable 0 and the second variable 1, ie as if it were using them in every action

    
asked by Victor Alvarado 05.02.2017 в 18:55
source

1 answer

1

for and while

when a for(int i=0; i<contenedor.length; i++) is used the iteration is based on the value of an additional variable, in this case i therefore it is completely dependent on i the tasks to be performed within the for , is say that to access the collection and manipulate it, it will depend on the value of i , the same happens with while , in addition that i can refer to non-existent elements so the error IndexOut

why use foreach?

In this case it is not dependent on any additional variables to the context of the problem's logic, but actually works directly with existing elements of the collection.

    
answered by 05.02.2017 / 19:21
source