for each && for | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

for each && for

when do we use for each instead of for?

20th Nov 2020, 9:36 AM
saeedeh
2 Answers
+ 5
For loop allows you to modify elements in an array but for each loop doesn't since it is only meant for traversing. Also you need to set an index in for loop to access elements and it enables random access of elements possible but you cannot do that with for each loop. For each loop is faster and efficient when it comes to large sets of data.
20th Nov 2020, 10:53 AM
Avinesh
Avinesh - avatar
+ 6
String[] array=//arrays of strings normal foreach, - for(String str:array){ System.out.print(str); } Stream foreach, -Arrays.stream(arr).forEach(i - > System.out.print(i));
20th Nov 2020, 10:06 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar