{"id":38978,"date":"2024-11-26T14:56:03","date_gmt":"2024-11-26T06:56:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38978\/"},"modified":"2024-11-26T14:56:03","modified_gmt":"2024-11-26T06:56:03","slug":"java%e6%80%8e%e4%b9%88%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38978\/","title":{"rendered":"java\u600e\u4e48\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u4e3a\u6bcf\u4e2a\u5143\u7d20\u5355\u72ec\u8d4b\u503c\u3002\u6570\u7ec4\u5b57\u9762\u91cf\uff1a\u4e00\u6b21\u6027\u4e3a\u6240\u6709\u5143\u7d20\u8d4b\u503c\u3002\u5faa\u73af\u8d4b\u503c\uff1a\u6309\u987a\u5e8f\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u3002arrays.fill() \u65b9\u6cd5\uff1a\u586b\u5145\u6240\u6709\u5143\u7d20\u7ed9\u5b9a\u503c\u3002system.arraycopy() \u65b9\u6cd5\uff1a\u4ece\u53e6\u4e00\u4e2a\u6570\u7ec4\u590d\u5236\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111520524389551.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4e3a Java \u4e2d\u7684\u8d4b\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u76f4\u63a5\u8d4b\u503c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5355\u72ec\u8d4b\u503c\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] names = {\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6765\u4e00\u6b21\u6027\u4e3a\u6240\u6709\u5143\u7d20\u8d4b\u503c\uff1a<\/p>\n<pre>String[] names = new String[] {\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c<\/strong><\/p>\n<p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u6309\u987a\u5e8f\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\uff1a<\/p>\n<pre>String[] names = new String[3];\nfor (int i = 0; i &lt; names.length; i++) {\n    names[i] = \"Name\" + (i + 1);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.fill() \u65b9\u6cd5\u53ef\u4ee5\u4e3a\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u586b\u5145\u7ed9\u5b9a\u7684\u503c\uff1a<\/p>\n<pre>String[] names = new String[3];\nArrays.fill(names, \"Default Name\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u7ec4\u590d\u5236\u5143\u7d20\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<pre>String[] sourceNames = {\"John\", \"Mary\", \"Bob\"};\nString[] destinationNames = new String[3];\nSystem.arraycopy(sourceNames, 0, destinationNames, 0, sourceNames.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u7ec4\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u6765\u5bb9\u7eb3\u6240\u6709\u5143\u7d20\u3002<\/li>\n<li>\u6570\u7ec4\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u3002<\/li>\n<li>\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u9ed8\u8ba4\u503c\u662f null\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u4e3a\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 \u4e2d\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u6cd5\uff1a\u76f4\u63a5\u8d4b\u503c\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u4e3a\u6bcf\u4e2a\u5143\u7d20\u5355\u72ec\u8d4b\u503c\u3002\u6570\u7ec4\u5b57\u9762\u91cf\uff1a\u4e00\u6b21\u6027\u4e3a\u6240\u6709\u5143\u7d20\u8d4b\u503c\u3002\u5faa\u73af\u8d4b\u503c\uff1a\u6309\u987a\u5e8f\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u3002arrays.fill() \u65b9\u6cd5\uff1a\u586b\u5145\u6240\u6709\u5143\u7d20\u7ed9\u5b9a\u503c\u3002system.arraycopy() \u65b9\u6cd5\uff1a\u4ece\u53e6\u4e00\u4e2a\u6570\u7ec4\u590d\u5236\u5143\u7d20\u3002 \u5982\u4f55\u4e3a Java \u4e2d\u7684\u8d4b\u503c \u5728 Java \u4e2d\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u8d4b\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u76f4\u63a5\u8d4b\u503c \u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5355\u72ec\u8d4b\u503c\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf \u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6765\u4e00\u6b21\u6027\u4e3a\u6240\u6709\u5143\u7d20\u8d4b\u503c\uff1a String[] names = new String[] {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c \u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u6309\u987a\u5e8f\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\uff1a String[] names = new String[3]; for (int i = 0; i &lt; names.length; i++) { [&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-38978","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38978","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=38978"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38978\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}