{"id":275431,"date":"2024-05-16T14:27:24","date_gmt":"2024-05-16T06:27:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/275431.html"},"modified":"2024-05-16T14:27:33","modified_gmt":"2024-05-16T06:27:33","slug":"java-%e7%bc%96%e7%a8%8b%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%9a%84-for-%e5%be%aa%e7%8e%af%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/275431.html","title":{"rendered":"java \u7f16\u7a0b\u9879\u76ee\u4e2d\u7684 for \u5faa\u73af\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200409\/f17c0f0f-d4e5-40bc-81b5-e16c21ed41a4.webp\" alt=\"java \u7f16\u7a0b\u9879\u76ee\u4e2d\u7684 for \u5faa\u73af\u600e\u4e48\u4f7f\u7528\" \/><\/p>\n<p><p>for\u5faa\u73af\u5728Java\u7f16\u7a0b\u9879\u76ee\u4e2d\u662f\u7528\u6765\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u5757\u76f4\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u6761\u4ef6\u88ab\u6ee1\u8db3\u3002\u5728Java\u4e2d\uff0cfor\u5faa\u73af\u6709\u4e09\u79cd\u4e3b\u8981\u5f62\u5f0f\uff1a\u6807\u51c6\u7684for\u5faa\u73af\u3001\u589e\u5f3a\u578bfor\u5faa\u73af (\u4e5f\u79f0\u4e3afor-each\u5faa\u73af)\u3001\u4ee5\u53caJava 8\u5f15\u5165\u7684\u6d41\u5f0ffor\u5faa\u73af\u3002<strong>\u6807\u51c6for\u5faa\u73af<\/strong>\u7528\u4e8e\u6309\u7167\u6307\u5b9a\u7684\u6b21\u6570\u6267\u884c\u4ee3\u7801\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2a\u521d\u59cb\u5316\u53d8\u91cf\u3001\u4e00\u4e2a\u5faa\u73af\u7ee7\u7eed\u7684\u6761\u4ef6\u4ee5\u53ca\u4e00\u4e2a\u8fed\u4ee3\u8bed\u53e5\uff1b<strong>\u589e\u5f3a\u578bfor\u5faa\u73af<\/strong>\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u6216\u96c6\u5408\u7c7b\u578b\uff1b<strong>\u6d41\u5f0ffor\u5faa\u73af<\/strong>\u5219\u5728\u5904\u7406\u96c6\u5408\u7c7b\u7684\u5143\u7d20\u65f6\u5141\u8bb8\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u548c\u66f4\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u6807\u51c6for\u5faa\u73af\u4e3a\u4f8b\uff0c\u5176\u7ed3\u6784\u5305\u62ec\u521d\u59cb\u5316\u90e8\u5206\u3001\u6761\u4ef6\u68c0\u6d4b\u90e8\u5206\u548c\u8fed\u4ee3\u90e8\u5206\uff0c\u683c\u5f0f\u5982\u4e0b:<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">for (\u521d\u59cb\u5316; \u5e03\u5c14\u8868\u8fbe\u5f0f; \u6b65\u8fdb) {<\/p>\n<p>    \/\/ \u5faa\u73af\u5185\u5bb9<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u521d\u59cb\u5316\u90e8\u5206\u521b\u5efa\u5faa\u73af\u53d8\u91cf\uff0c\u5728\u5faa\u73af\u6761\u4ef6\u90e8\u5206\u8bbe\u7f6e\u5faa\u73af\u6301\u7eed\u7684\u6761\u4ef6\uff0c\u5728\u8fed\u4ee3\u90e8\u5206\u5b9a\u4e49\u6bcf\u6b21\u5faa\u73af\u540e\u5faa\u73af\u53d8\u91cf\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6807\u51c6FOR\u5faa\u73af\u7684\u683c\u5f0f\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u521d\u59cb\u5316<\/h4>\n<\/p>\n<p><p>for\u5faa\u73af\u5f00\u59cb\u4e4b\u524d\u4f1a\u6267\u884c\u521d\u59cb\u5316\u90e8\u5206\uff0c\u5b83\u901a\u5e38\u7528\u4e8e\u58f0\u660e\u5faa\u73af\u8ba1\u6570\u5668\u3002\u521d\u59cb\u5316\u8bed\u53e5\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><h4>\u6761\u4ef6\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u5728\u6bcf\u6b21\u8fed\u4ee3\u5f00\u59cb\u4e4b\u524d\uff0c\u4f1a\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002\u5982\u679c\u6761\u4ef6\u4e3atrue\uff0c\u5faa\u73af\u4f53\u5c06\u88ab\u6267\u884c\uff1b\u5982\u679c\u4e3afalse\uff0c\u5faa\u73af\u5c06\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h4>\u6b65\u8fdb<\/h4>\n<\/p>\n<p><p>\u6bcf\u6b21\u5faa\u73af\u540e\uff0c\u6b65\u8fdb\u8bed\u53e5\u4f1a\u88ab\u6267\u884c\uff0c\u901a\u5e38\u7528\u4e8e\u66f4\u65b0\u5faa\u73af\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><h4>\u5faa\u73af\u4f53<\/h4>\n<\/p>\n<p><p>\u5728\u6ee1\u8db3\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\uff0c\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u589e\u5f3a\u578bFOR\u5faa\u73af\u7684\u683c\u5f0f\u4e0e\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><h4>\u683c\u5f0f\u8bf4\u660e<\/h4>\n<\/p>\n<p><p>\u589e\u5f3a\u578bfor\u5faa\u73af\u7528\u4e8e\u66f4\u7b80\u6d01\u5730\u904d\u5386\u6570\u7ec4\u6216Collections\u6846\u67b6\u4e2d\u7684\u96c6\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">for (\u58f0\u660e\u8bed\u53e5 : \u8868\u8fbe\u5f0f) {<\/p>\n<p>    \/\/ \u5faa\u73af\u4f53<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684\u8868\u8fbe\u5f0f\u901a\u5e38\u662f\u8981\u904d\u5386\u7684\u6570\u7ec4\u6216\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\u548c\u4e00\u4e2aArrayList\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u4ed6\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">int[] numbers = {1, 2, 3, 4, 5, 6};<\/p>\n<p>for (int item : numbers) {<\/p>\n<p>    System.out.println(item);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6d41\u5f0fFOR\u5faa\u73af\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><h4>\u4f18\u52bf\u8bf4\u660e<\/h4>\n<\/p>\n<p><p>\u968f\u7740Java 8\u7684\u5f15\u5165\uff0c\u6d41API\u589e\u6dfb\u4e86\u8bb8\u591a\u65b0\u7684\u7279\u6027\uff0c\u5141\u8bb8\u5e76\u884c\u5904\u7406\u96c6\u5408\u548c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b\u6f14\u793a<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6d41\u7684\u65b9\u5f0f\u6765\u8fed\u4ee3\u96c6\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">List&lt;String&gt; myList = Arrays.asList(&quot;a1&quot;, &quot;a2&quot;, &quot;b1&quot;, &quot;c2&quot;, &quot;c1&quot;);<\/p>\n<p>myList.stream()<\/p>\n<p>    .filter(s -&gt; s.startsWith(&quot;c&quot;))<\/p>\n<p>    .map(String::toUpperCase)<\/p>\n<p>    .sorted()<\/p>\n<p>    .forEach(System.out::println);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001FOR\u5faa\u73af\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>for\u5faa\u73af\u5e38\u7528\u4e8e\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u904d\u5386\u5904\u7406\uff0c\u4f8b\u5982\u8bfb\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6216\u8005\u96c6\u5408\u4e2d\u7684\u5bf9\u8c61\uff0c\u5e76\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u6539\u53d8\u5668\u8fed\u4ee3<\/h4>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\uff0c\u5f53\u4f7f\u7528\u8fed\u4ee3\u5668\u6a21\u5f0f\u904d\u5386\u96c6\u5408\u65f6\uff0c\u5f80\u5f80\u4e5f\u4f7f\u7528for\u5faa\u73af\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>\u5faa\u73af\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u65f6\uff0c\u5408\u7406\u7684\u63a7\u5236\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u53d8\u91cf\u81f3\u5173\u91cd\u8981\uff0c\u8fd9\u80fd\u907f\u514d\u65e0\u9650\u5faa\u73af\u548c\u610f\u5916\u7684\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u5bf9\u5927\u578b\u96c6\u5408\u4f7f\u7528for\u5faa\u73af\u65f6\uff0c\u8003\u8651\u6027\u80fd\u4f18\u5316\uff1a\u4f8b\u5982\uff0c\u907f\u514d\u5728\u5faa\u73af\u5185\u90e8\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u6216\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u53ef\u8bfb\u6027\u4e0e\u7b80\u6d01\u6027<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u4e5f\u662f\u91cd\u8981\u7684\u3002\u65e0\u8bba\u4f55\u79cd\u7c7b\u578b\u7684\u5faa\u73af\uff0c\u786e\u4fdd\u4ee3\u7801\u6574\u6d01\u5e76\u4e14\u5bb9\u6613\u7406\u89e3\uff0c\u5c24\u5176\u662f\u5728\u6709\u591a\u91cd\u5d4c\u5957\u5faa\u73af\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cfor\u5faa\u73af\u5728Java\u9879\u76ee\u4e2d\u7684\u4f7f\u7528\u5341\u5206\u5e7f\u6cdb\uff0c\u638c\u63e1\u5176\u5404\u79cd\u5f62\u5f0f\u548c\u6700\u4f73\u5b9e\u8df5\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Java\u7f16\u7a0b\u9879\u76ee\u4e2d\u6b63\u786e\u4f7f\u7528for\u5faa\u73af\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4ec0\u4e48\u662ffor\u5faa\u73af\u53ca\u5176\u8bed\u6cd5\u7ed3\u6784\uff1f<\/strong><br \/>For\u5faa\u73af\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u5b83\u901a\u8fc7\u6307\u5b9a\u5faa\u73af\u53d8\u91cf\u7684\u521d\u59cb\u503c\u3001\u5faa\u73af\u6761\u4ef6\u548c\u6bcf\u6b21\u5faa\u73af\u540e\u7684\u64cd\u4f5c\u6765\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u5b83\u7684\u8bed\u6cd5\u7ed3\u6784\u901a\u5e38\u4e3a\uff1a<code>for (\u521d\u59cb\u5316; \u5faa\u73af\u6761\u4ef6; \u8fed\u4ee3\u64cd\u4f5c) { \u5faa\u73af\u4f53 }<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u7528for\u5faa\u73af\u904d\u5386\u6570\u7ec4\u6216\u96c6\u5408\uff1f<\/strong><br \/>\u5982\u679c\u8981\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\u6216\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u6574\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u9010\u4e2a\u8bbf\u95ee\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java\">int[] nums = {1, 2, 3, 4, 5};\nfor (int i = 0; i &lt; nums.length; i++) {\n    System.out.println(nums[i]);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u5d4c\u5957\u5faa\u73af\uff1f<\/strong><br \/>\u5728Java\u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u8fdb\u884c\u591a\u91cd\u5faa\u73af\u6765\u5904\u7406\u590d\u6742\u7684\u903b\u8f91\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u7684\u5d4c\u5957\u5f62\u5f0f\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u6253\u5370\u4e00\u4e2a\u4e5d\u4e5d\u4e58\u6cd5\u8868\uff1a<\/p>\n<pre><code class=\"language-java\">for (int i = 1; i &lt;= 9; i++) {\n    for (int j = 1; j &lt;= i; j++) {\n        System.out.print(j + &quot; * &quot; + i + &quot; = &quot; + (i * j) + &quot;  &quot;);\n    }\n    System.out.println();\n}\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u5d4c\u5957\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u641c\u7d22\u7279\u5b9a\u7684\u5143\u7d20\u3001\u751f\u6210\u56fe\u5f62\u7b49\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u662f\u5173\u4e8e\u5728Java\u7f16\u7a0b\u9879\u76ee\u4e2d\u6b63\u786e\u4f7f\u7528for\u5faa\u73af\u7684\u4e00\u4e9b\u4ecb\u7ecd\u548c\u793a\u4f8b\u3002\u8bf7\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\u548c\u8bed\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u9879\u76ee\u7684\u529f\u80fd\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"for\u5faa\u73af\u5728Java\u7f16\u7a0b\u9879\u76ee\u4e2d\u662f\u7528\u6765\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u5757\u76f4\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u6761\u4ef6\u88ab\u6ee1\u8db3\u3002\u5728Java\u4e2d\uff0cfor\u5faa\u73af\u6709\u4e09\u79cd [&hellip;]","protected":false},"author":3,"featured_media":275443,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275431"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=275431"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/275443"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=275431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=275431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=275431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}