{"id":36981,"date":"2024-11-26T15:10:59","date_gmt":"2024-11-26T07:10:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36981\/"},"modified":"2024-11-26T15:10:59","modified_gmt":"2024-11-26T07:10:59","slug":"java%e7%a9%ba%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e6%89%a9%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36981\/","title":{"rendered":"java\u7a7a\u6570\u7ec4\u600e\u4e48\u6269\u5bb9"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6269\u5bb9\u7a7a\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 arrays.copyof() \u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u586b\u5145\u539f\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528 system.arraycopy() \u5c06\u7a7a\u6570\u7ec4\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\uff0c\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u957f\u5ea6\u3002\u7f16\u5199\u81ea\u5b9a\u4e49\u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4\u7684\u4e24\u500d\uff0c\u5e76\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111222403037515.jpg\" class=\"aligncenter\" title=\"java\u7a7a\u6570\u7ec4\u600e\u4e48\u6269\u5bb9\u63d2\u56fe\" alt=\"java\u7a7a\u6570\u7ec4\u600e\u4e48\u6269\u5bb9\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u7a7a\u6570\u7ec4\u6269\u5bb9\u65b9\u6cd5<\/strong><\/p>\n<p>\u5f53 Java \u6570\u7ec4\u4e3a\u7a7a\u65f6\uff0c\u65e0\u6cd5\u76f4\u63a5\u63d2\u5165\u5143\u7d20\u3002\u9700\u8981\u5148\u6269\u5bb9\u6570\u7ec4\uff0c\u624d\u80fd\u6dfb\u52a0\u5143\u7d20\u3002Java \u4e2d\u6269\u5bb9\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5728\u65b0\u6570\u7ec4\u4e2d\u586b\u5145\u539f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u539f\u6570\u7ec4\u4e3a\u7a7a\uff0c\u5219\u65b0\u6570\u7ec4\u5c06\u662f\u6307\u5b9a\u7684\u957f\u5ea6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = new int[0]; \/\/ \u7a7a\u6570\u7ec4\narr = Arrays.copyOf(arr, 10); \/\/ \u590d\u5236\u7a7a\u6570\u7ec4\u5230\u957f\u5ea6\u4e3a 10 \u7684\u65b0\u6570\u7ec4<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u5c06\u6307\u5b9a\u6e90\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\u3002\u5bf9\u4e8e\u7a7a\u6570\u7ec4\uff0c\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u6e90\u6570\u7ec4\uff0c\u6307\u5b9a\u76ee\u6807\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u957f\u5ea6\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002<\/p>\n<pre>int[] arr = new int[0]; \/\/ \u7a7a\u6570\u7ec4\nint[] newArr = new int[10];\nSystem.arraycopy(arr, 0, newArr, 0, 0); \/\/ \u5c06\u7a7a\u6570\u7ec4\u7684 0 \u4e2a\u5143\u7d20\u590d\u5236\u5230 newArr \u4e2d<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u81ea\u5b9a\u4e49\u6269\u5bb9\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u65b9\u6cd5\u6765\u6269\u5bb9\u6570\u7ec4\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684\u4e24\u500d\uff0c\u7136\u540e\u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>public static int[] expand(int[] arr) {\n    int[] newArr = new int[arr.length * 2];\n    System.arraycopy(arr, 0, newArr, 0, arr.length);\n    return newArr;\n}\n\nint[] arr = new int[0]; \/\/ \u7a7a\u6570\u7ec4\narr = expand(arr); \/\/ \u6269\u5bb9\u7a7a\u6570\u7ec4\u5230\u957f\u5ea6\u4e3a 0<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u7a7a\u6570\u7ec4\u600e\u4e48\u6269\u5bb9\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\u6269\u5bb9\u7a7a\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 arrays.copyof() \u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u586b\u5145\u539f\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528 system.arraycopy() \u5c06\u7a7a\u6570\u7ec4\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\uff0c\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u957f\u5ea6\u3002\u7f16\u5199\u81ea\u5b9a\u4e49\u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4\u7684\u4e24\u500d\uff0c\u5e76\u590d\u5236\u539f\u6570\u7ec4\u5143\u7d20\u3002 Java \u7a7a\u6570\u7ec4\u6269\u5bb9\u65b9\u6cd5 \u5f53 Java \u6570\u7ec4\u4e3a\u7a7a\u65f6\uff0c\u65e0\u6cd5\u76f4\u63a5\u63d2\u5165\u5143\u7d20\u3002\u9700\u8981\u5148\u6269\u5bb9\u6570\u7ec4\uff0c\u624d\u80fd\u6dfb\u52a0\u5143\u7d20\u3002Java \u4e2d\u6269\u5bb9\u6570\u7ec4\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5 Arrays.copyOf() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5728\u65b0\u6570\u7ec4\u4e2d\u586b\u5145\u539f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u539f\u6570\u7ec4\u4e3a\u7a7a\uff0c\u5219\u65b0\u6570\u7ec4\u5c06\u662f\u6307\u5b9a\u7684\u957f\u5ea6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = new int[0]; \/\/ \u7a7a\u6570\u7ec4 arr = Arrays.copyOf(arr, 10); \/\/ \u590d\u5236\u7a7a\u6570\u7ec4\u5230\u957f\u5ea6\u4e3a 10 \u7684\u65b0\u6570\u7ec4 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5 System.arraycopy() \u65b9\u6cd5\u5c06\u6307\u5b9a\u6e90\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4\u4e2d\u3002\u5bf9\u4e8e\u7a7a\u6570\u7ec4\uff0c\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u6e90\u6570\u7ec4\uff0c\u6307\u5b9a\u76ee\u6807\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u957f\u5ea6\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002 int[] arr = new int[0]; \/\/ \u7a7a\u6570\u7ec4 int[] newArr = new int[10]; System.arraycopy(arr, 0, [&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-36981","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36981","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=36981"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36981\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}