Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about MalaGupta's Review Note 5.9 (Enhanced for)  RSS feed

 
Winston Liek
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accdg to the book:

[javadoc]The enhanced for loop can’t be used to iterate over multiple collections or
arrays in the same loop.[/javadoc]

It does not provide any explanation/examples there..

Can someone please provide an example or explanation please enlighten me.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take these arrays as an example:


With a regular for loop you could write something like the following code snippet to print both arrays. This is not possible using the enhanced for loop. And of course the same applies if I had used collections instead of arrays:


Hope it helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!