How does the Java for each loop work

0 votes

Consider:

List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
    System.out.println(item);
}

What would the equivalent for loop look like without using the for each syntax?

Dec 22, 2020 in Java by Rajiv
• 8,910 points
384 views

1 answer to this question.

0 votes
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

Also, if the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length

answered Dec 22, 2020 by Gitika
• 65,910 points

Related Questions In Java

0 votes
0 answers

How does the Java 'for each' loop work?

Consider: List<String> someList = new ArrayList<String>(); // add "monkey", ...READ MORE

Apr 19, 2022 in Java by Rahul
• 3,380 points
226 views
0 votes
3 answers

How does the “final” keyword in Java work?

Final is a keyword that is used to ...READ MORE

answered Sep 1, 2019 in Java by DEEPAK KUMAR GUPTA
1,894 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
676 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,910 points
956 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,120 points
581 views
0 votes
1 answer

What is a name function in JavaScript & how to define it?

A named function declares a name as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
4,244 views
0 votes
1 answer

What is $route service in AngularJs?

$route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 10, 2020 in Angular by Niroj
• 82,880 points
1,003 views
0 votes
1 answer

What are the $route service Events provide in AngularJs?

hey, $route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 10, 2020 in Angular by Niroj
• 82,880 points
885 views
0 votes
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,520 points
203,932 views
+1 vote
13 answers

How does java.net.SocketException: Connection reset happen ?

You can use wireshark to view the ...READ MORE

answered Dec 7, 2018 in Java by tushh
249,183 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP