Solved

Enhanced For loop vs ForEach Loop

Posted on 2016-09-04
4
110 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 35
AutoIncrement column based of FK 11 55
Java program running SQL query 5 37
swing controls 2 16
A short article about problems I had with the new location API and permissions in Marshmallow
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

856 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