{"id":34513,"date":"2024-11-26T08:24:34","date_gmt":"2024-11-26T00:24:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34513\/"},"modified":"2024-11-26T08:24:34","modified_gmt":"2024-11-26T00:24:34","slug":"java%e6%80%8e%e4%b9%88%e5%af%b9%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34513\/","title":{"rendered":"java\u600e\u4e48\u5bf9\u5bf9\u8c61\u6570\u7ec4\u521d\u59cb\u5316"},"content":{"rendered":"<blockquote><p>\n  \u56de\u7b54\uff1a java \u5bf9\u8c61\u6570\u7ec4\u7684\u521d\u59cb\u5316\u65b9\u5f0f\u5305\u62ec\u6570\u7ec4\u5b57\u9762\u91cf\u3001new \u5173\u952e\u5b57\u3001arrays.fill() \u65b9\u6cd5\u3001\u5faa\u73af\u548c stream api\u3002\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\uff1a\u521b\u5efa\u5e26\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f7f\u7528 new \u5173\u952e\u5b57\uff1a\u521b\u5efa\u5e26\u6709 null \u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1a\u7528\u7279\u5b9a\u503c\u586b\u5145\u6570\u7ec4\u3002\u4f7f\u7528\u5faa\u73af\uff1a\u9010\u4e2a\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4\u3002\u4f7f\u7528 stream api\uff1a\u4ece\u5217\u8868\u521b\u5efa\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110215332794294.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5bf9\u5bf9\u8c61\u6570\u7ec4\u521d\u59cb\u5316\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5bf9\u5bf9\u8c61\u6570\u7ec4\u521d\u59cb\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5bf9 Java \u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316<\/strong><\/p>\n<p>Java \u4e2d\u7684\u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf<\/strong><\/p>\n<pre>String[] names = {\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528 new \u5173\u952e\u5b57<\/strong><\/p>\n<pre>Integer[] numbers = new Integer[5];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a null \u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<pre>int[] scores = new int[10];\nArrays.fill(scores, 0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 10 \u4e2a\u503c\u5747\u4e3a 0 \u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>4. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<pre>String[] cities = new String[3];\nfor (int i = 0; i &lt; cities.length; i++) {\n    cities[i] = \"City\" + (i + 1);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 3 \u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee5 &#8220;City&#8221; \u4e3a\u524d\u7f00\uff0c\u5e76\u9644\u52a0\u4e00\u4e2a\u6570\u5b57\u540e\u7f00\u3002<\/p>\n<p><strong>5. \u4f7f\u7528 Stream API<\/strong><\/p>\n<pre>List&lt;String&gt; names = Arrays.asList(\"John\", \"Mary\", \"Bob\");\nString[] namesArray = names.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 3 \u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u5143\u7d20\u4e0e List \u4e2d\u7684\u5143\u7d20\u76f8\u540c\u3002<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u8c61\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u5f15\u7528\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u81ea\u5b9a\u4e49\u7c7b\u3002<\/li>\n<li>\u6570\u7ec4\u7684\u957f\u5ea6\u4e00\u65e6\u88ab\u521d\u59cb\u5316\uff0c\u5c31\u65e0\u6cd5\u4fee\u6539\u3002<\/li>\n<li>\u5728\u521d\u59cb\u5316\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u5c06\u9ed8\u8ba4\u4e3a Object\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u5bf9\u8c61\u6570\u7ec4\u521d\u59cb\u5316\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>\u56de\u7b54\uff1a java \u5bf9\u8c61\u6570\u7ec4\u7684\u521d\u59cb\u5316\u65b9\u5f0f\u5305\u62ec\u6570\u7ec4\u5b57\u9762\u91cf\u3001new \u5173\u952e\u5b57\u3001arrays.fill() \u65b9\u6cd5\u3001\u5faa\u73af\u548c stream api\u3002\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\uff1a\u521b\u5efa\u5e26\u6709\u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f7f\u7528 new \u5173\u952e\u5b57\uff1a\u521b\u5efa\u5e26\u6709 null \u5143\u7d20\u7684\u6570\u7ec4\u3002\u4f7f\u7528 arrays.fill() \u65b9\u6cd5\uff1a\u7528\u7279\u5b9a\u503c\u586b\u5145\u6570\u7ec4\u3002\u4f7f\u7528\u5faa\u73af\uff1a\u9010\u4e2a\u5143\u7d20\u521d\u59cb\u5316\u6570\u7ec4\u3002\u4f7f\u7528 stream api\uff1a\u4ece\u5217\u8868\u521b\u5efa\u6570\u7ec4\u3002 \u5982\u4f55\u5bf9 Java \u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316 Java \u4e2d\u7684\u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\uff1a 1. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528 new \u5173\u952e\u5b57 Integer[] numbers = new Integer[5]; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a null \u5143\u7d20\u7684\u6570\u7ec4\u3002 3. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5 int[] scores = new int[10]; [&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-34513","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34513","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=34513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}