{"id":38613,"date":"2024-11-26T09:21:27","date_gmt":"2024-11-26T01:21:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38613\/"},"modified":"2024-11-26T09:21:27","modified_gmt":"2024-11-26T01:21:27","slug":"%e6%80%8e%e4%b9%88%e7%90%86%e8%a7%a3java%e4%b8%ad%e7%9a%84%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38613\/","title":{"rendered":"\u600e\u4e48\u7406\u89e3java\u4e2d\u7684\u904d\u5386\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u904d\u5386\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff1afor \u5faa\u73af\uff1a\u6309\u7d22\u5f15\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1bforeach \u5faa\u73af\uff1a\u8bed\u6cd5\u66f4\u7b80\u6d01\u7684\u589e\u5f3a for \u5faa\u73af\uff1bjava 8 \u6d41\uff1a\u4e00\u79cd\u9ad8\u7ea7\u3001\u51fd\u6570\u5f0f\u7684\u904d\u5386\u65b9\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111503521442179.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u7406\u89e3java\u4e2d\u7684\u904d\u5386\u6570\u7ec4\u63d2\u56fe\" alt=\"\u600e\u4e48\u7406\u89e3java\u4e2d\u7684\u904d\u5386\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u904d\u5386\u6570\u7ec4\u662f\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u8fc7\u7a0b\u3002Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u6570\u7ec4\uff1a<\/p>\n<p><strong>for \u5faa\u73af<\/strong><\/p>\n<p>\u6700\u5e38\u89c1\u7684\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 for \u5faa\u73af\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\n\nfor (int i = 0; i &lt; numbers.length; i++) {\n  System.out.println(numbers[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor \u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ece\u7d22\u5f15 0 \u5230 numbers.length-1\u3002\u5b83\u5c06\u6253\u5370\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>foreach \u5faa\u73af<\/strong><\/p>\n<p>Java 5 \u5f15\u5165\u4e86 foreach \u5faa\u73af\uff0c\u53c8\u79f0\u589e\u5f3a for \u5faa\u73af\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u904d\u5386\u6570\u7ec4\uff1a<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\n\nfor (int number : numbers) {\n  System.out.println(number);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a foreach \u5faa\u73af\u6267\u884c\u4e0e\u4e0a\u9762\u7684 for \u5faa\u73af\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u8bed\u6cd5\u66f4\u7b80\u6d01\u3002<\/p>\n<p><strong>Java 8 \u6d41<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86\u6d41\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u3001\u66f4\u51fd\u6570\u5f0f\u7684\u904d\u5386\u6570\u7ec4\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\n\nArrays.stream(numbers)\n  .forEach(System.out::println);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u6d41\u8868\u8fbe\u5f0f\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u7528 forEach() \u65b9\u6cd5\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u7ec4\u7684\u5927\u5c0f\u3001\u9700\u8981\u6267\u884c\u7684\u64cd\u4f5c\u4ee5\u53ca\u6240\u9700\u7684\u3002for \u5faa\u73af\u662f\u6700\u7075\u6d3b\u7684\uff0c\u5141\u8bb8\u5bf9\u6570\u7ec4\u8fdb\u884c\u81ea\u5b9a\u4e49\u63a7\u5236\uff0c\u4f46 foreach \u5faa\u73af\u548c\u6d41\u8868\u8fbe\u5f0f\u901a\u5e38\u66f4\u7b80\u6d01\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7406\u89e3java\u4e2d\u7684\u904d\u5386\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u904d\u5386\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff1afor \u5faa\u73af\uff1a\u6309\u7d22\u5f15\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1bforeach \u5faa\u73af\uff1a\u8bed\u6cd5\u66f4\u7b80\u6d01\u7684\u589e\u5f3a for \u5faa\u73af\uff1bjava 8 \u6d41\uff1a\u4e00\u79cd\u9ad8\u7ea7\u3001\u51fd\u6570\u5f0f\u7684\u904d\u5386\u65b9\u5f0f\u3002 Java \u4e2d\u904d\u5386\u6570\u7ec4 \u904d\u5386\u6570\u7ec4\u662f\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u8fc7\u7a0b\u3002Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u6570\u7ec4\uff1a for \u5faa\u73af \u6700\u5e38\u89c1\u7684\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 for \u5faa\u73af\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i &lt; numbers.length; i++) { System.out.println(numbers[i]); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor \u5faa\u73af\u5c06\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ece\u7d22\u5f15 0 \u5230 numbers.length-1\u3002\u5b83\u5c06\u6253\u5370\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002 foreach \u5faa\u73af Java 5 \u5f15\u5165\u4e86 foreach \u5faa\u73af\uff0c\u53c8\u79f0\u589e\u5f3a for \u5faa\u73af\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u904d\u5386\u6570\u7ec4\uff1a int[] numbers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38613","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38613"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38613\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}