{"id":65462,"date":"2025-05-03T13:46:37","date_gmt":"2025-05-03T05:46:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65462\/"},"modified":"2025-05-03T13:46:37","modified_gmt":"2025-05-03T05:46:37","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9a%e4%b9%89%e4%b8%8d%e5%ae%9a%e9%95%bf%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65462\/","title":{"rendered":"java\u600e\u4e48\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u4e0d\u5b9a\u957f\u6570\u7ec4\uff0c\u53c8\u79f0\u53ef\u53d8\u957f\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u3002\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\uff1a1. \u4f7f\u7528 new \u5173\u952e\u5b57\u8bed\u6cd5\uff1b2. \u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\uff1b3. \u4f7f\u7528 arraylist \u7c7b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111301334444570.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49 Java \u4e2d\u7684\u4e0d\u5b9a\u957f\u6570\u7ec4<\/strong><\/p>\n<p>\u4e0d\u5b9a\u957f\u6570\u7ec4\uff0c\u4e5f\u79f0\u4e3a\u53ef\u53d8\u957f\u6570\u7ec4\uff0c\u662f\u4e00\u79cd\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u7684\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 new \u5173\u952e\u5b57<\/strong><\/p>\n<p>\u8bed\u6cd5\uff1aint[] arr = new int[size];<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>arr \u662f\u4e0d\u5b9a\u957f\u6570\u7ec4\u7684\u540d\u79f0\u3002<\/li>\n<li>size \u662f\u8981\u521b\u5efa\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8bed\u6cd5\uff1aint[] arr = Arrays.copyOf(originalArray, newSize);<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>originalArray \u662f\u8981\u8c03\u6574\u5927\u5c0f\u7684\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n<li>newSize \u662f\u65b0\u7684\u6570\u7ec4\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528 ArrayList \u7c7b<\/strong><\/p>\n<p>\u8bed\u6cd5\uff1aArrayList arr = new ArrayList(); <\/p>\n<p>ArrayList \u7c7b\u652f\u6301\u52a8\u6001\u5927\u5c0f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr1 = new int[5]; \/\/ \u521b\u5efa\u5927\u5c0f\u4e3a 5 \u7684\u4e0d\u5b9a\u957f\u6570\u7ec4\n\nint[] arr2 = Arrays.copyOf(arr1, 10); \/\/ \u5c06 arr1 \u7684\u5927\u5c0f\u8c03\u6574\u4e3a 10\n\nArrayList&lt;Integer&gt; arr3 = new ArrayList&lt;&gt;(); \/\/ \u521b\u5efa\u4e00\u4e2a ArrayList \u6570\u7ec4<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e0d\u5b9a\u957f\u6570\u7ec4\u5728\u5b9a\u4e49\u540e\u4e0d\u80fd\u518d\u6539\u53d8\u5927\u5c0f\u3002<\/li>\n<li>\u4e0d\u5b9a\u957f\u6570\u7ec4\u7684\u5143\u7d20\u9ed8\u8ba4\u521d\u59cb\u5316\u4e3a 0\uff08\u5bf9\u4e8e\u6570\u5b57\u7c7b\u578b\uff09\u6216 null\uff08\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff09\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u5b9a\u957f\u6570\u7ec4\uff0c\u53c8\u79f0\u53ef\u53d8\u957f\u6570\u7ec4\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5927\u5c0f\u3002\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\uff1a1. \u4f7f\u7528 new \u5173\u952e\u5b57\u8bed\u6cd5\uff1b2. \u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\uff1b3. \u4f7f\u7528 arraylist \u7c7b\u3002 \u5982\u4f55\u5b9a\u4e49 Java \u4e2d\u7684\u4e0d\u5b9a\u957f\u6570\u7ec4 \u4e0d\u5b9a\u957f\u6570\u7ec4\uff0c\u4e5f\u79f0\u4e3a\u53ef\u53d8\u957f\u6570\u7ec4\uff0c\u662f\u4e00\u79cd\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u7684\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49\u4e0d\u5b9a\u957f\u6570\u7ec4\uff1a 1. \u4f7f\u7528 new \u5173\u952e\u5b57 \u8bed\u6cd5\uff1aint[] arr = new int[size]; \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5176\u4e2d\uff1a arr \u662f\u4e0d\u5b9a\u957f\u6570\u7ec4\u7684\u540d\u79f0\u3002 size \u662f\u8981\u521b\u5efa\u7684\u5143\u7d20\u6570\u91cf\u3002 2. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5 \u8bed\u6cd5\uff1aint[] arr = Arrays.copyOf(originalArray, newSize); \u5176\u4e2d\uff1a originalArray \u662f\u8981\u8c03\u6574\u5927\u5c0f\u7684\u539f\u59cb\u6570\u7ec4\u3002 newSize \u662f\u65b0\u7684\u6570\u7ec4\u5927\u5c0f\u3002 3. \u4f7f\u7528 ArrayList \u7c7b \u8bed\u6cd5\uff1aArrayList arr = new [&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-65462","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65462","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=65462"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65462\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}