{"id":34810,"date":"2024-11-26T14:04:23","date_gmt":"2024-11-26T06:04:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34810\/"},"modified":"2024-11-26T14:04:23","modified_gmt":"2024-11-26T06:04:23","slug":"java%e6%80%8e%e4%b9%88%e5%9c%a8%e6%95%b0%e7%bb%84%e9%87%8c%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34810\/","title":{"rendered":"java\u600e\u4e48\u5728\u6570\u7ec4\u91cc\u6dfb\u52a0\u5143\u7d20"},"content":{"rendered":"<blockquote><p>\n  \u867d\u7136 java \u6570\u7ec4\u4e0d\u53ef\u53d8\uff0c\u65e0\u6cd5\u76f4\u63a5\u6dfb\u52a0\u5143\u7d20\uff0c\u4f46\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u89e3\u51b3\u6b64\u95ee\u9898\uff1a\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u8ffd\u52a0\u65b0\u5143\u7d20\u3002\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a arraylist\uff0c\u4f7f\u7528 add() \u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110303423542520.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5728\u6570\u7ec4\u91cc\u6dfb\u52a0\u5143\u7d20\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5728\u6570\u7ec4\u91cc\u6dfb\u52a0\u5143\u7d20\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5411\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>Java \u4e2d\u6570\u7ec4\u662f\u4e00\u7ec4\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u6570\u636e\u5143\u7d20\u7684\u56fa\u5b9a\u5927\u5c0f\u5bb9\u5668\u3002\u867d\u7136 Java \u6570\u7ec4\u7684\u957f\u5ea6\u5728\u521b\u5efa\u540e\u65e0\u6cd5\u66f4\u6539\uff0c\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u4f60\u65e0\u6cd5\u5411\u73b0\u6709\u6570\u7ec4\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3};\nint[] newArr = Arrays.copyOf(arr, arr.length + 1);\nnewArr[newArr.length - 1] = 4;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 newArr\uff0c\u5176\u5927\u5c0f\u6bd4\u539f\u59cb\u6570\u7ec4 arr \u5927\u4e00\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5411 newArr \u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u503c 4\u3002<\/p>\n<p><strong>\u4f7f\u7528 ArrayList<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u9009\u62e9\u662f\u5148\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a ArrayList\uff0c\u7136\u540e\u4f7f\u7528\u5176 add() \u65b9\u6cd5\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3};\nList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(arr));\nlist.add(4);\n\n\/\/ \u5c06 ArrayList \u8f6c\u6362\u56de\u6570\u7ec4\nint[] newArr = list.stream().mapToInt(Integer::intValue).toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a ArrayList list \u5e76\u5c06\u5176\u4ece\u6570\u7ec4 arr \u590d\u5236\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 add() \u65b9\u6cd5\u5411 list \u4e2d\u6dfb\u52a0\u503c 4\u3002\u6700\u540e\uff0c\u4f7f\u7528 stream() \u548c toArray() \u65b9\u6cd5\u5c06 list \u8f6c\u6362\u56de\u4e00\u4e2a\u6570\u7ec4 newArr\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>Java \u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u76f4\u63a5\u5411\u73b0\u6709\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u3002<\/li>\n<li>\u5728\u4e0d\u91cd\u65b0\u521b\u5efa\u6570\u7ec4\u7684\u60c5\u51b5\u4e0b\u5411\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<li>\u59cb\u7ec8\u8003\u8651\u6570\u7ec4\u7684\u5927\u5c0f\u5e76\u786e\u4fdd\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u6765\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5728\u6570\u7ec4\u91cc\u6dfb\u52a0\u5143\u7d20\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>\u867d\u7136 java \u6570\u7ec4\u4e0d\u53ef\u53d8\uff0c\u65e0\u6cd5\u76f4\u63a5\u6dfb\u52a0\u5143\u7d20\uff0c\u4f46\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u89e3\u51b3\u6b64\u95ee\u9898\uff1a\u4f7f\u7528 arrays.copyof() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u8ffd\u52a0\u65b0\u5143\u7d20\u3002\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a arraylist\uff0c\u4f7f\u7528 add() \u65b9\u6cd5\u6dfb\u52a0\u5143\u7d20\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 \u5982\u4f55\u5728 Java \u4e2d\u5411\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20 Java \u4e2d\u6570\u7ec4\u662f\u4e00\u7ec4\u5177\u6709\u76f8\u540c\u7c7b\u578b\u7684\u6570\u636e\u5143\u7d20\u7684\u56fa\u5b9a\u5927\u5c0f\u5bb9\u5668\u3002\u867d\u7136 Java \u6570\u7ec4\u7684\u957f\u5ea6\u5728\u521b\u5efa\u540e\u65e0\u6cd5\u66f4\u6539\uff0c\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u4f60\u65e0\u6cd5\u5411\u73b0\u6709\u6570\u7ec4\u6dfb\u52a0\u65b0\u5143\u7d20\u3002 \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3}; int[] newArr = Arrays.copyOf(arr, arr.length + 1); newArr[newArr.length &#8211; 1] = 4; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 newArr\uff0c\u5176\u5927\u5c0f\u6bd4\u539f\u59cb\u6570\u7ec4 arr \u5927\u4e00\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5411 newArr \u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u503c 4\u3002 \u4f7f\u7528 ArrayList \u53e6\u4e00\u79cd\u9009\u62e9\u662f\u5148\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a ArrayList\uff0c\u7136\u540e\u4f7f\u7528\u5176 add() \u65b9\u6cd5\uff1a int[] [&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-34810","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34810","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=34810"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34810\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}