{"id":37135,"date":"2024-11-26T12:36:01","date_gmt":"2024-11-26T04:36:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37135\/"},"modified":"2024-11-26T12:36:01","modified_gmt":"2024-11-26T04:36:01","slug":"java%e6%80%8e%e4%b9%88%e4%b8%ba%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37135\/","title":{"rendered":"java\u600e\u4e48\u4e3a\u6570\u7ec4\u5982\u4f55\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u8d4b\u503c\u6709\u4e94\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u521d\u59cb\u5316\u3001\u4f7f\u7528 for \u5faa\u73af\u3001\u4f7f\u7528 arrays.fill()\u3001\u4f7f\u7528 system.arraycopy() \u548c\u4f7f\u7528\u5de5\u5177\u7c7b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111301542534303.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u4e3a\u6570\u7ec4\u5982\u4f55\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u4e3a\u6570\u7ec4\u5982\u4f55\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u4e3a\u6570\u7ec4\u8d4b\u503c<\/strong><\/p>\n<p>\u4e3a Java \u6570\u7ec4\u8d4b\u503c\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u521d\u59cb\u5316<\/strong><\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5728\u6570\u7ec4\u58f0\u660e\u65f6\u540c\u65f6\u8d4b\u503c\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 for \u5faa\u73af<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u4e3a\u5143\u7d20\u8d4b\u503c\uff1a<\/p>\n<pre>int[] arr = new int[5];\nfor (int i = 0; i &lt; arr.length; i++) {\n    arr[i] = i + 1;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill()<\/strong><\/p>\n<p>Arrays.fill() \u65b9\u6cd5\u53ef\u4ee5\u4e3a\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8d4b\u503c\u76f8\u540c\u7684\u503c\uff1a<\/p>\n<pre>int[] arr = new int[5];\nArrays.fill(arr, 5);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>int[] arr1 = {1, 2, 3};\nint[] arr2 = new int[arr1.length];\nSystem.arraycopy(arr1, 0, arr2, 0, arr1.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528\u5de5\u5177\u7c7b<\/strong><\/p>\n<p>\u4e00\u4e9b\u5916\u90e8\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4fbf\u5229\u7684\u65b9\u6cd5\u6765\u4e3a\u6570\u7ec4\u8d4b\u503c\u3002\u4f8b\u5982\uff0cGuava \u7684 Lists.newArrayList() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5143\u7d20\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>int[] arr = Lists.newArrayList(1, 2, 3).toArray(new int[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u4e3a\u6570\u7ec4\u5982\u4f55\u8d4b\u503c\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 \u6570\u7ec4\u8d4b\u503c\u6709\u4e94\u79cd\u65b9\u6cd5\uff1a\u76f4\u63a5\u521d\u59cb\u5316\u3001\u4f7f\u7528 for \u5faa\u73af\u3001\u4f7f\u7528 arrays.fill()\u3001\u4f7f\u7528 system.arraycopy() \u548c\u4f7f\u7528\u5de5\u5177\u7c7b\u3002 Java \u4e2d\u4e3a\u6570\u7ec4\u8d4b\u503c \u4e3a Java \u6570\u7ec4\u8d4b\u503c\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u76f4\u63a5\u521d\u59cb\u5316 \u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5728\u6570\u7ec4\u58f0\u660e\u65f6\u540c\u65f6\u8d4b\u503c\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5}; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 for \u5faa\u73af \u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u9010\u4e2a\u4e3a\u5143\u7d20\u8d4b\u503c\uff1a int[] arr = new int[5]; for (int i = 0; i &lt; arr.length; i++) { arr[i] = i + 1; } \u767b\u5f55\u540e\u590d\u5236 3. [&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-37135","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37135","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=37135"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37135\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}