{"id":40315,"date":"2024-11-26T16:30:18","date_gmt":"2024-11-26T08:30:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40315\/"},"modified":"2024-11-26T16:30:18","modified_gmt":"2024-11-26T08:30:18","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e9%81%8d%e5%8e%86%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40315\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u904d\u5386 java \u5b57\u7b26\u4e32\u6570\u7ec4\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 for \u5faa\u73af\uff1afor (int i = 0; i <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111817244259219.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u904d\u5386<\/strong><\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p><strong>for \u5faa\u73af<\/strong><\/p>\n<p>\u6700\u5e38\u7528\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>String[] names = {\"Alice\", \"Bob\", \"Cindy\"};\n\nfor (int i = 0; i &lt; names.length; i++) {\n    String name = names[i];\n\n    \/\/ \u5bf9 name \u6267\u884c\u64cd\u4f5c...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fed\u4ee3\u5668<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u6570\u7ec4\uff1a<\/p>\n<pre>String[] names = {\"Alice\", \"Bob\", \"Cindy\"};\n\nIterator&lt;String&gt; iterator = Arrays.asList(names).iterator();\n\nwhile (iterator.hasNext()) {\n    String name = iterator.next();\n\n    \/\/ \u5bf9 name \u6267\u884c\u64cd\u4f5c...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Java 8+\uff1aforEach<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86 forEach() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7b80\u5316\u904d\u5386\u64cd\u4f5c\uff1a<\/p>\n<pre>String[] names = {\"Alice\", \"Bob\", \"Cindy\"};\n\nArrays.stream(names).forEach(name -&gt; {\n    \/\/ \u5bf9 name \u6267\u884c\u64cd\u4f5c...\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>Spliterator\uff1a<\/strong>\u7528\u4e8e\u5e76\u884c\u5904\u7406\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li> <strong>Stream\uff1a<\/strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\uff0c\u5141\u8bb8\u4f7f\u7528\u6d41\u64cd\u4f5c\uff08\u5982\u8fc7\u6ee4\u548c\u6620\u5c04\uff09\u8fdb\u884c\u904d\u5386\u3002<\/li>\n<li> <strong>Arrays.sort()\uff1a<\/strong>\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 for \u5faa\u73af\u6216\u8fed\u4ee3\u5668\u904d\u5386\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u904d\u5386\u4e00\u4e2a\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>\u904d\u5386 java \u5b57\u7b26\u4e32\u6570\u7ec4\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 for \u5faa\u73af\uff1afor (int i = 0; i Java \u4e2d\u904d\u5386 \u89e3\u7b54\uff1a for \u5faa\u73af \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 for \u5faa\u73af\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = {&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Cindy&#8221;}; for (int i = 0; i &lt; names.length; i++) { String name = names[i]; \/\/ \u5bf9 name \u6267\u884c\u64cd\u4f5c&#8230; } \u767b\u5f55\u540e\u590d\u5236 \u8fed\u4ee3\u5668 \u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u6570\u7ec4\uff1a String[] names = {&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Cindy&#8221;}; Iterator&lt;String&gt; iterator [&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-40315","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40315","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=40315"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40315\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}