{"id":223,"date":"2007-09-20T00:00:00","date_gmt":"2007-09-20T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=223"},"modified":"2007-09-20T00:00:00","modified_gmt":"2007-09-20T00:00:00","slug":"Using Enhanced For-Loops with Your Classes","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=223","title":{"rendered":"Using Enhanced For-Loops with Your Classes"},"content":{"rendered":"<p><P>The enhanced for-loop is a popular feature introduced with the Java SE platform in version 5.0. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array\/collection without explicitly expressing how one goes from element to element. <\/P><br \/>\n<P>Because the old style of coding didn&#8217;t become invalid with the new for-loop syntax, you don&#8217;t have to use an enhanced for-loop when visiting each element of an array\/collection. However, with the new style, one&#8217;s code would typically change from something like the following: <\/P><br \/>\n<P><CODE>for&nbsp;(int&nbsp;i=0;&nbsp;i&lt;array.length;&nbsp;i++)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Element:&nbsp;&#8221;&nbsp;+&nbsp;array[i]);<\/CODE><BR><CODE>}<\/CODE><BR><BR><CODE>to&nbsp;the&nbsp;newer&nbsp;form:<\/CODE><BR><BR><CODE>for&nbsp;(String&nbsp;element&nbsp;:&nbsp;array)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Element:&nbsp;&#8221;&nbsp;+&nbsp;element);<\/CODE><BR><CODE>}<\/CODE><BR><\/P><br \/>\n<P>Assuming &#8220;array&#8221; is defined to be an array of <CODE>String<\/CODE> objects, each element is assigned to the element variable as it loops through the array. These basics of the enhanced for-loop were covered in an earlier Tech Tip: <A href=\"http:\/\/communications1.sun.com\/r\/c\/r?2.1.3J1.2Vc.11Fua%5f.C4sP1g..H.EhM8.1nTk.DcJAEaU0\" target=_blank rel=nofollow>The Enhanced For Loop<\/A>, from May 5, 2005.<\/P><br \/>\n<P>If you have a class called <CODE>Colony<\/CODE> which contains a group of <CODE>Penguin<\/CODE> objects, without doing anything extra to get the enhanced for-loop to work, one way you would loop through each penguin element would be to return an <CODE>Iterator<\/CODE> and iterate through the colony. Unfortunately, the enhanced for-loop does not work with <CODE>Iterator<\/CODE> , so the following won&#8217;t even compile: <\/P><BR><CODE>\/\/&nbsp;Does&nbsp;not&nbsp;compile<\/CODE><BR><CODE>import&nbsp;java.util.*;<\/CODE><BR><CODE>public&nbsp;class&nbsp;BadColony&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;static&nbsp;class&nbsp;Penguin&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin(String&nbsp;name)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name&nbsp;=&nbsp;name;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;toString()&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&#8220;Penguin{&#8221;&nbsp;+&nbsp;name&nbsp;+&nbsp;&#8220;}&#8221;;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;Set&lt;Penguin&gt;&nbsp;set&nbsp;=&nbsp;new&nbsp;HashSet&lt;Penguin&gt;();<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;void&nbsp;addPenguin(Penguin&nbsp;p)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;set.add(p);<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;Iterator&lt;Penguin&gt;&nbsp;getPenguins()&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;set.iterator();<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[])&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Colony&nbsp;colony&nbsp;=&nbsp;new&nbsp;Colony();<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;opus&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Opus&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;chilly&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Chilly&nbsp;Willy&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;mumble&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Mumble&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;emperor&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Emperor&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(opus);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(chilly);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(mumble);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(emperor);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Iterator&lt;Penguin&gt;&nbsp;it&nbsp;=&nbsp;colony.getPenguins();<\/CODE><BR><CODE>\/\/&nbsp;The&nbsp;bad&nbsp;line&nbsp;of&nbsp;code:<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Penguin&nbsp;p&nbsp;:&nbsp;it)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><CODE>}<\/CODE><BR><br \/>\n<P>You cannot just pass an <CODE>Iterator<\/CODE> into the enhanced for-loop. The 2nd line of the following will generate a compilation error: <\/P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Iterator&lt;Penguin&gt;&nbsp;it&nbsp;=&nbsp;colony.getPenguins();<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Penguin&nbsp;p&nbsp;:&nbsp;it)&nbsp;{<\/CODE><BR><BR>The&nbsp;error:<BR><BR><CODE>BadColony.java:36:&nbsp;foreach&nbsp;not&nbsp;applicable&nbsp;to&nbsp;expression&nbsp;type<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Penguin&nbsp;p&nbsp;:&nbsp;it)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^<\/CODE><BR><CODE>1&nbsp;error<\/CODE><BR><br \/>\n<P>In order to be able to use your class with an enhanced for-loop, it does need an <CODE>Iterator<\/CODE> , but that <CODE>Iterator<\/CODE> must be provided via the <CODE>Iterable<\/CODE> interface: <\/P><BR><CODE>public&nbsp;interface&nbsp;java.lang.Iterable&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;java.util.Iterator&nbsp;iterator();<\/CODE><BR><CODE>}<\/CODE><BR><br \/>\n<P>Actually, to be more correct, you can use a generic <CODE>T<\/CODE> , allowing the enhanced for-loop to avoid casting, returning the designated generic type, instead of just a plain old <CODE>Object<\/CODE> . <\/P><CODE>public&nbsp;interface&nbsp;java.lang.Iterable&lt;T&gt;&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;java.util.Iterator&lt;T&gt;&nbsp;iterator();<\/CODE><BR><CODE>}<\/CODE><BR><br \/>\n<P>It is this <CODE>Iterable<\/CODE> object which is then provided to the enhanced for-loop. By making the <CODE>Colony<\/CODE> class implement <CODE>Iterable<\/CODE> , and having its new <CODE>iterator()<\/CODE> method return the <CODE>Iterator<\/CODE> that <CODE>getPenguins()<\/CODE> provides, you&#8217;ll be able to loop through the penguins in the colony via an enhanced for-loop. <\/P><br \/>\n<P>By adding the proper implements clause:<\/P><CODE>public&nbsp;class&nbsp;Colony&nbsp;implements&nbsp;Iterable&lt;Colony.Penguin&gt;&nbsp;{<\/CODE><BR><BR><BR>You&nbsp;then&nbsp;get&nbsp;your&nbsp;enhanced&nbsp;for-loop&nbsp;for&nbsp;the&nbsp;colony:<BR><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Penguin&nbsp;p&nbsp;:&nbsp;colony)&nbsp;{<\/CODE><BR><BR>Here&#8217;s&nbsp;the&nbsp;updated&nbsp;<CODE>Colony<\/CODE> &nbsp;class&nbsp;with&nbsp;the&nbsp;corrected&nbsp;code:<BR><BR><CODE>import&nbsp;java.util.*;<\/CODE><BR><BR><CODE>public&nbsp;class&nbsp;Colony&nbsp;implements&nbsp;Iterable&lt;Colony.Penguin&gt;&nbsp;{<\/CODE><BR><BR><CODE>&nbsp;&nbsp;static&nbsp;class&nbsp;Penguin&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin(String&nbsp;name)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name&nbsp;=&nbsp;name;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;String&nbsp;toString()&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&#8220;Penguin{&#8221;&nbsp;+&nbsp;name&nbsp;+&nbsp;&#8220;}&#8221;;<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;Set&lt;Penguin&gt;&nbsp;set&nbsp;=&nbsp;new&nbsp;HashSet&lt;Penguin&gt;();<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;void&nbsp;addPenguin(Penguin&nbsp;p)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;set.add(p);<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;Iterator&lt;Penguin&gt;&nbsp;getPenguins()&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;set.iterator();<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;Iterator&lt;Penguin&gt;&nbsp;iterator()&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;getPenguins();<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><BR><CODE>&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[])&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Colony&nbsp;colony&nbsp;=&nbsp;new&nbsp;Colony();<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;opus&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Opus&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;chilly&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Chilly&nbsp;Willy&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;mumble&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Mumble&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;Penguin&nbsp;emperor&nbsp;=&nbsp;new&nbsp;Penguin(&#8220;Emperor&#8221;);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(opus);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(chilly);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(mumble);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;colony.addPenguin(emperor);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Penguin&nbsp;p&nbsp;:&nbsp;colony)&nbsp;{<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p);<\/CODE><BR><CODE>&nbsp;&nbsp;&nbsp;&nbsp;}<\/CODE><BR><CODE>&nbsp;&nbsp;}<\/CODE><BR><CODE>}<\/CODE><BR><BR>Running&nbsp;the&nbsp;code&nbsp;produces&nbsp;the&nbsp;following&nbsp;output:<BR><BR><CODE>&nbsp;&nbsp;&gt;&nbsp;java&nbsp;Colony<\/CODE><BR><BR><CODE>&nbsp;&nbsp;Penguin{Chilly&nbsp;Willy}<\/CODE><BR><CODE>&nbsp;&nbsp;Penguin{Mumble}<\/CODE><BR><CODE>&nbsp;&nbsp;Penguin{Opus}<\/CODE><BR><CODE>&nbsp;&nbsp;Penguin{Emperor}<\/CODE><BR><br \/>\n<P>Keep in mind that the individual penguins are internally kept in a Set type collection so the returned order doesn&#8217;t necessarily match the insertion order, which in this case it doesn&#8217;t. <\/P><br \/>\n<P>Remember to genericize the implements clause for the class &#8220;<CODE>implements Iterable&lt;T&gt;<\/CODE> &#8221; and not just say &#8220;<CODE>implements Iterable<\/CODE> &#8220;. With the latter, the enhanced for-loop will only return an <CODE>Object<\/CODE> for each element. <\/P><br \/>\n<P>For more information on the enhanced for-loop, please see the <A href=\"http:\/\/communications1.sun.com\/r\/c\/r?2.1.3J1.2Vc.11Fua%5f.C4sP1g..H.EhMA.1nTk.DdCKEad0\" target=_blank rel=nofollow>Java Programming Language guide from JDK 1.5<\/A>.<\/P><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The enhanced for-loop is a popular feature introduced with the Java SE platform in version 5.0. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array\/collection without explicitly expressing how one goes from element to element. Because the old style of coding didn&#8217;t become invalid with the &hellip; <a href=\"https:\/\/www.strongd.net\/?p=223\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Enhanced For-Loops with Your Classes<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}