{"id":6515,"date":"2024-04-21T23:38:38","date_gmt":"2024-04-22T06:38:38","guid":{"rendered":"https:\/\/zentut.com\/java-tutorial\/java-foreach\/"},"modified":"2024-04-21T23:39:21","modified_gmt":"2024-04-22T06:39:21","slug":"java-foreach","status":"publish","type":"page","link":"https:\/\/www.zentut.com\/java-tutorial\/java-foreach\/","title":{"rendered":"Java Foreach"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Java Foreach loop to iterate over elements of an array or collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Java Foreach loop<\/h2>\n\n\n\n<p>To iterate over elements of an <a href=\"https:\/\/zentut.com\/java-tutorial\/java-array\/\">array<\/a>, you often use a <code><a href=\"https:\/\/zentut.com\/java-tutorial\/java-for-loop\/\">for<\/a><\/code> loop. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">double<\/span>&#91;] amounts = { <span class=\"hljs-number\">9.99<\/span>, <span class=\"hljs-number\">10.25<\/span>, <span class=\"hljs-number\">7.89<\/span> };\n\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; amounts.length; i++) {\n   System.out.println(amounts&#91;i]);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>for<\/code> loop:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, initialize a loop variable <code>i<\/code> that represents the index of the element of the array. <\/li>\n\n\n\n<li>Second, access the array element by the index <code>i<\/code>, display it, and increase the loop variable <code>i<\/code> by one to advance to the next index in each iteration.<\/li>\n\n\n\n<li>Third, continue the loop as long as the loop variable <code>i<\/code> is less than the number of elements of the array.<\/li>\n<\/ul>\n\n\n\n<p>The <code>for<\/code> loop works fine. But it has some issues:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Index management<\/strong>: You need to manage the loop variable, which serves as the array index, yourself. If you do it incorrectly, you&#8217;ll get an <a href=\"https:\/\/zentut.com\/java-tutorial\/java-try-catch\/\">exception<\/a>.<\/li>\n\n\n\n<li><strong>Code clutter<\/strong>: The <code>for<\/code> loop makes the code more cluttered, especially with complex logic within the loop body.<\/li>\n\n\n\n<li><strong>Limited abstraction<\/strong>: The <code>for<\/code> loop exposes very low-level control over the iteration process.<\/li>\n<\/ul>\n\n\n\n<p>To address these issues, Java 8 introduced an enhanced <code>for<\/code> loop, which is often called a Foreach loop.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the Foreach loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">for<\/span>(dataType element: array) {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dataType<\/code> specifies the data type of the element of the array.<\/li>\n\n\n\n<li><code>element<\/code> is a variable that holds each element in the array during iteration.<\/li>\n\n\n\n<li><code>array<\/code> is the array to be iterated.<\/li>\n<\/ul>\n\n\n\n<p>Since Java can infer the type of the array element, you can use the <a href=\"https:\/\/zentut.com\/java-tutorial\/java-var\/\"><code>var<\/code> keyword<\/a> to make the code more concise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">for<\/span>(<span class=\"hljs-keyword\">var<\/span> element: array) {\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the iterate over elements of the <code>amounts<\/code> array above, you can use a Foreach loop as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">double<\/span>&#91;] amounts = {<span class=\"hljs-number\">9.99<\/span>, <span class=\"hljs-number\">10.25<\/span>, <span class=\"hljs-number\">7.89<\/span>};\n\n<span class=\"hljs-keyword\">for<\/span>(<span class=\"hljs-keyword\">var<\/span> amount: amounts) {\n    System.out.println(amount);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a Foreach loop to iterate over elements of an array or a collection to make code more concise.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Java Foreach loop to iterate over elements of an array or collection.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":2190,"menu_order":24,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6515","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/comments?post=6515"}],"version-history":[{"count":2,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6515\/revisions"}],"predecessor-version":[{"id":6517,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/6515\/revisions\/6517"}],"up":[{"embeddable":true,"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/pages\/2190"}],"wp:attachment":[{"href":"https:\/\/www.zentut.com\/wp-json\/wp\/v2\/media?parent=6515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}