{"id":39105,"date":"2024-11-26T15:48:34","date_gmt":"2024-11-26T07:48:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39105\/"},"modified":"2024-11-26T15:48:34","modified_gmt":"2024-11-26T07:48:34","slug":"java-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e5%8a%a0%e5%85%a5%e6%95%b0%e7%bb%84%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39105\/","title":{"rendered":"java \u5b57\u7b26\u4e32\u600e\u4e48\u52a0\u5165\u6570\u7ec4\u4e2d"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6709\u4e24\u79cd\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff1a\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1a\u4f7f\u7528\u503c\u5217\u8868\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u5143\u7d20\u3002arrays.copyof() \u65b9\u6cd5\uff1a\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5176\u5185\u5bb9\u4e0e\u6307\u5b9a\u6570\u7ec4\u76f8\u540c\uff0c\u53ef\u7528\u4e8e\u6dfb\u52a0\u5b57\u7b26\u4e32\u5230\u73b0\u6709\u6570\u7ec4\u6216\u521b\u5efa\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111604040095491.jpg\" class=\"aligncenter\" title=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u52a0\u5165\u6570\u7ec4\u4e2d\u63d2\u56fe\" alt=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u52a0\u5165\u6570\u7ec4\u4e2d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u52a0\u5165 Java \u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668<\/strong><\/p>\n<p>\u6570\u7ec4\u521d\u59cb\u5316\u5668\u662f\u4e00\u79cd\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u4f7f\u7528\u503c\u5217\u8868\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] array = { \"\u5b57\u7b26\u4e32 1\", \"\u5b57\u7b26\u4e32 2\", \"\u5b57\u7b26\u4e32 3\" };<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u521b\u5efa\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5185\u5bb9\u4e0e\u6307\u5b9a\u6570\u7ec4\u76f8\u540c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u73b0\u6709\u6570\u7ec4\u6216\u521b\u5efa\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>String[] newArray = Arrays.copyOf(oldArray, newArrayLength);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>oldArray \u662f\u73b0\u6709\u6570\u7ec4\u6216\u60a8\u5e0c\u671b\u6dfb\u52a0\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002<\/li>\n<li>newArrayLength \u662f\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5b83\u5fc5\u987b\u5927\u4e8e\u6216\u7b49\u4e8e oldArray \u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n<p>\u8981\u5c06\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u73b0\u6709\u6570\u7ec4\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 Arrays.copyOf() \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u4f7f newArrayLength \u6bd4 oldArray \u957f\u5ea6\u5927 1\u3002<\/li>\n<li>\u5c06 oldArray \u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u5c06\u8981\u6dfb\u52a0\u7684\u5b57\u7b26\u4e32\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>String[] oldArray = { \"\u5b57\u7b26\u4e32 1\", \"\u5b57\u7b26\u4e32 2\" };\nString[] newArray = Arrays.copyOf(oldArray, oldArray.length + 1);\nnewArray[newArray.length - 1] = \"\u5b57\u7b26\u4e32 3\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 newArray\uff0c\u5b83\u5305\u542b oldArray \u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u4ee5\u53ca\u65b0\u6dfb\u52a0\u7684\u5b57\u7b26\u4e32 &#8220;\u5b57\u7b26\u4e32 3&#8243;\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u5b57\u7b26\u4e32\u600e\u4e48\u52a0\u5165\u6570\u7ec4\u4e2d\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\u6709\u4e24\u79cd\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff1a\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1a\u4f7f\u7528\u503c\u5217\u8868\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u5143\u7d20\u3002arrays.copyof() \u65b9\u6cd5\uff1a\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5176\u5185\u5bb9\u4e0e\u6307\u5b9a\u6570\u7ec4\u76f8\u540c\uff0c\u53ef\u7528\u4e8e\u6dfb\u52a0\u5b57\u7b26\u4e32\u5230\u73b0\u6709\u6570\u7ec4\u6216\u521b\u5efa\u65b0\u6570\u7ec4\u3002 \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u52a0\u5165 Java \u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff1a 1. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668 \u6570\u7ec4\u521d\u59cb\u5316\u5668\u662f\u4e00\u79cd\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u4f7f\u7528\u503c\u5217\u8868\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u52a0\u5165\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] array = { &#8220;\u5b57\u7b26\u4e32 1&#8221;, &#8220;\u5b57\u7b26\u4e32 2&#8221;, &#8220;\u5b57\u7b26\u4e32 3&#8221; }; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u6cd5\u5c06\u521b\u5efa\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\u3002 2. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5 Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5185\u5bb9\u4e0e\u6307\u5b9a\u6570\u7ec4\u76f8\u540c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u73b0\u6709\u6570\u7ec4\u6216\u521b\u5efa\u5305\u542b\u65b0\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a String[] newArray = Arrays.copyOf(oldArray, newArrayLength); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a oldArray \u662f\u73b0\u6709\u6570\u7ec4\u6216\u60a8\u5e0c\u671b\u6dfb\u52a0\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002 newArrayLength \u662f\u65b0\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5b83\u5fc5\u987b\u5927\u4e8e\u6216\u7b49\u4e8e oldArray \u7684\u957f\u5ea6\u3002 \u8981\u5c06\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u73b0\u6709\u6570\u7ec4\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u4f7f\u7528 Arrays.copyOf() \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u4f7f newArrayLength \u6bd4 oldArray \u957f\u5ea6\u5927 1\u3002 \u5c06 [&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-39105","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39105","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=39105"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39105\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}