Question 10: "How does the Java ’for each’ loop work?"

Java - Top 10 Most Viewed Questions on Stack Overflow Question 10: "How does the Java ’for each’ loop work?"
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.60
List Price:  €92.30
You save:  €27.69
£55.40
List Price:  £79.14
You save:  £23.74
CA$95.14
List Price:  CA$135.92
You save:  CA$40.78
A$107.21
List Price:  A$153.17
You save:  A$45.95
S$94.29
List Price:  S$134.71
You save:  S$40.41
HK$547.55
List Price:  HK$782.26
You save:  HK$234.70
CHF 63.39
List Price:  CHF 90.56
You save:  CHF 27.17
NOK kr753.41
List Price:  NOK kr1,076.34
You save:  NOK kr322.93
DKK kr481.90
List Price:  DKK kr688.47
You save:  DKK kr206.56
NZ$116.54
List Price:  NZ$166.49
You save:  NZ$49.95
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,699.83
List Price:  ৳11,000.23
You save:  ৳3,300.40
₹5,834.28
List Price:  ₹8,335.05
You save:  ₹2,500.76
RM331.26
List Price:  RM473.25
You save:  RM141.99
₦97,755.03
List Price:  ₦139,656.03
You save:  ₦41,901
₨19,509.87
List Price:  ₨27,872.43
You save:  ₨8,362.56
฿2,546.30
List Price:  ฿3,637.73
You save:  ฿1,091.43
₺2,255.86
List Price:  ₺3,222.80
You save:  ₺966.93
B$348.35
List Price:  B$497.67
You save:  B$149.31
R1,321.80
List Price:  R1,888.37
You save:  R566.56
Лв126.46
List Price:  Лв180.67
You save:  Лв54.20
₩94,455.35
List Price:  ₩134,942
You save:  ₩40,486.65
₪256.23
List Price:  ₪366.06
You save:  ₪109.83
₱3,937.46
List Price:  ₱5,625.18
You save:  ₱1,687.72
¥10,582.69
List Price:  ¥15,118.78
You save:  ¥4,536.09
MX$1,164.09
List Price:  MX$1,663.06
You save:  MX$498.96
QR255.79
List Price:  QR365.44
You save:  QR109.64
P961.07
List Price:  P1,373.02
You save:  P411.94
KSh9,203.68
List Price:  KSh13,148.68
You save:  KSh3,945
E£3,306.33
List Price:  E£4,723.53
You save:  E£1,417.20
ብር3,986
List Price:  ብር5,694.53
You save:  ብር1,708.53
Kz58,526.40
List Price:  Kz83,612.73
You save:  Kz25,086.33
CLP$68,613.99
List Price:  CLP$98,024.19
You save:  CLP$29,410.20
CN¥505.72
List Price:  CN¥722.49
You save:  CN¥216.77
RD$4,149.44
List Price:  RD$5,928.03
You save:  RD$1,778.58
DA9,428.56
List Price:  DA13,469.95
You save:  DA4,041.39
FJ$157.67
List Price:  FJ$225.25
You save:  FJ$67.58
Q547.27
List Price:  Q781.84
You save:  Q234.57
GY$14,690.75
List Price:  GY$20,987.69
You save:  GY$6,296.93
ISK kr9,671.91
List Price:  ISK kr13,817.61
You save:  ISK kr4,145.70
DH707.37
List Price:  DH1,010.57
You save:  DH303.20
L1,234.61
List Price:  L1,763.81
You save:  L529.19
ден3,973.74
List Price:  ден5,677.01
You save:  ден1,703.27
MOP$565.34
List Price:  MOP$807.66
You save:  MOP$242.32
N$1,327.13
List Price:  N$1,895.98
You save:  N$568.85
C$2,582.33
List Price:  C$3,689.21
You save:  C$1,106.87
रु9,350.11
List Price:  रु13,357.88
You save:  रु4,007.76
S/259.70
List Price:  S/371.02
You save:  S/111.31
K268.50
List Price:  K383.60
You save:  K115.09
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,783.85
List Price:  ZK2,548.47
You save:  ZK764.61
L321.17
List Price:  L458.83
You save:  L137.66
Kč1,633.99
List Price:  Kč2,334.38
You save:  Kč700.38
Ft25,496.77
List Price:  Ft36,425.52
You save:  Ft10,928.74
SEK kr741.62
List Price:  SEK kr1,059.50
You save:  SEK kr317.88
ARS$59,963.93
List Price:  ARS$85,666.43
You save:  ARS$25,702.49
Bs486.54
List Price:  Bs695.09
You save:  Bs208.54
COP$272,760.10
List Price:  COP$389,673.99
You save:  COP$116,913.89
₡35,355.24
List Price:  ₡50,509.65
You save:  ₡15,154.41
L1,731.96
List Price:  L2,474.33
You save:  L742.37
₲515,624.88
List Price:  ₲736,638.54
You save:  ₲221,013.66
$U2,646.58
List Price:  $U3,781
You save:  $U1,134.41
zł278.71
List Price:  zł398.17
You save:  zł119.46
Already have an account? Log In

Transcript

The 10th question, also with over 2.5 million views is how does the Java for each loop work? And let's start with what isn't for each loop. For each loop syntactical appropriation, it supersedes the explicit use of the iterator of a given data structure. So let's think about the use case. You have a data structure like a list an array or something like this that stores several elements. And you want to iterate over each element of this data structure.

And before the for each loop and was announced in Java five, you had to use always the iterator and the iterator is Yeah, is an object to iterate through the data structure I element by element by element and so on till until you get to the end of the data structure and there aren't any more elements to iterate through. So let's take a look at the code before the for each loop was announced. So you had to create an iterator. And this object you want is the data structure we want to iterate through. So it is called some iterable iterable. And we call dot iterator and this method returns an iterator in our case, some iterable is of the type string.

So the elements of the data structure are strings. So the iterators also of type of type, string and date We asked the iterator iterator dot has next. So you are asking the iterator, do you have a next element to iterate through? And if the iterator says yes, Boolean true, then you can call iterator dot next to get the next element. And after you got the next element, then the pointer of the data structure or the iterator is increment by one. So it points now to the next element.

And then you ask the iterator again. Do you still have the next element? Yes, and so on. And at the end, the pointer? Yeah, doesn't have or doesn't point to an element because you you, you iterate through all elements and then iterator has next is false, and you leave the while loop and you iterated over. or through all elements of the data structure.

This is the iterator use with a while loop, you may use it in a for loop, it's exactly doing the same thing, but in two different ways. So in the for loop, you can also create first of all you create an iterator II. And the Yeah, you do that for loop runs as long as this expression is true, I dot has next. And within the for loop, you say e dot next to get the next element and to to increment a pointer to the next element. And for loop runs as long as this expression is true. So that's the for loop.

In my opinion, the while loop is a bit bit Yeah, easier to read. And it's nicer. But yeah, it depends on how what you want or what what do you like. But as the for each loop was announced in Java five, you don't need to use the iterator anymore. So you can now write this one this for loop for each loop and the syntax, it looks like that, that on the right side, you have to take a structure you want to iterate through, then you have two points here. And on the left side, you have one single element, in this case, string, and then the for each loop iterates through all the elements like here, above or here.

And yeah, you have the same the same work the All all three loops do the same work. But as you can see, the for each loop is the shortest one. And so I would say the for each loop as a syntactical abbreviation question now is what of what kind of requirement does the data structure on the right side has to accomplish that you may use it in a for each loop. So, you can't, you can't use any, any kind of object here in the for each loop. So there's a special requirement on this object and the requirement is the interface to vailable. So every class that should be usable in such a for each loop has to implement the interface it's available.

And if you take a look at the interface, what does the interface does, it has Method iterator that is abstract the A say it's abstract. So your class, or your type of data structure that implements iterable interface has to implement an override an iterator method on its own. And this method has to return an iterator of type T. So yeah, it looks pretty, pretty much the same like here. So some iterator dot iterator is called. And then you have iterator and you iterate through the data structure. And the for each loop class, effectively the same, but you don't see it.

So Java does it for you. Yeah, secretly and quietly. So if you do so, some words for each loop and you have some it's available. Java calls the iterator method from from some iterable on its own and so on. And it's exactly the same. So yeah, everything what what you need to do if to use for each loop with a data structure is that your data structure have to implement iterator or it's available interface.

And then you have to, to override the iterator method. And the for each and splits writer are default methods. So they have yet implementations and you don't have to, to provide any implementations for these two methods. There's one exception, arrays exception because arrays aren't a normal data types. So there isn't the class array. And that's why an array isn't able to implement the runnable interface.

But you can array you can use arrays in the in the for each loop as well. So some iterable may be an array as well. And this is also allowed. So, yeah, you have either arrays or data structures or objects, which implements iterable. So let's take it. Let's have a look at an example.

In my case, I didn't in that the code is in one surrounded class. And this surrounding class is has an embedded class. It's a private static class, my string container and what does my string container my string container has list of strings. And you can add strings to this container. It's a simple simple straight data structure where you can add strings to it and this string or this my string container should it should be able to to be used in a for each loop. So it has to to implement the iterable interface.

And why we because we do this we need an iterator method. And in this case, I've made it quick way, I am using the iterator of my internally data structure to store the strings from my string container. I am using internally the simple list in this case an ArrayList. And every list has an iterator so I can use the List iterator to iterate over my string container. And in to do so, I am creating a string container an empty string container, I add two strings to it. And then I am allowed to to use it in a for each loop.

So I can write my string container and string s and iterate through all the strings in my string container. If you want to take a look on the way it would look like this, you have my string container. You can call the iterator method to get an string iterator out of it. And then you has yeah iterator has next and iterator next, as we saw here at the first slide of the code. So let's have a quick look at an example. So We are in question 10.

And if I would, if I would delete the implements it's available from my string container, then I don't need to override the iterator method. And what kind of error do we get? We get this error can only iterate over an array or an instance of Java Lang iterable. So the for each loop doesn't work anymore. Because we aren't implementing the iterable interface anymore. And the error message is clear.

You can only iterate either of either an array or an instance of Java Lang iterable. So yeah, you see how to do this. implements iterator eligible type string in our case. And then we have this method here. iterator. We save it and everything is fine.

All right. That's how a for each loop books

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.