Solved

Enhanced For loop vs ForEach Loop

Posted on 2016-09-04
4
64 Views
Last Modified: 2016-09-05
Hi,

what are differences. practical uses of Enhanced For loop vs ForEach Loop. When to use which one. Why one is called external iterator and other as internal iterator, please advise
0
Comment
Question by:gudii9
  • 2
4 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41784241
In Java, the terms "enhanced for loop" and "foreach loop" refer to the same thing. Can you post a link to where you read that they were different?
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 41784386
for(int i=0;i<100;i++){
System.out.println(i);
}

Open in new window


... you are responsible for the halt condition.
int[] integers ={1,2,3};
for(int i: integers){System.out.println(i);
}

Open in new window

... Java handles the stop condition.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41785475
i mean java 8 for each with lamda

https://www.mkyong.com/java8/java-8-foreach-examples/
0
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
ID: 41785497
There are barely any differences between the two strategies, so use whichever one produces code that is easier for you to understand.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now