{"id":64348,"date":"2025-05-03T11:39:14","date_gmt":"2025-05-03T03:39:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64348\/"},"modified":"2025-05-03T11:39:14","modified_gmt":"2025-05-03T03:39:14","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%84%e6%b7%bb%e5%8a%a0%e5%85%83%e7%b4%a0%e6%80%8e%e4%b9%88%e5%8a%9e-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64348\/","title":{"rendered":"java\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u600e\u4e48\u529e"},"content":{"rendered":"<blockquote><p>\n  \u6570\u7ec4\u5728java\u4e2d\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u6dfb\u52a0\u5143\u7d20\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528arraylist\uff1a\u53ef\u53d8\u957f\u5ea6\u5217\u8868\uff0c\u652f\u6301\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u521b\u5efa\u65b0\u6570\u7ec4\uff1a\u6bd4\u539f\u59cb\u6570\u7ec4\u5927\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u518d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110306122934206.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u600e\u4e48\u529e\u63d2\u56fe\" alt=\"java\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u600e\u4e48\u529e\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u7ec4\u56fa\u5b9a\u957f\u5ea6\u7684\u4e0d\u53d8\u96c6\u5408\uff0c\u4e00\u65e6\u521b\u5efa\u540e\u5c31\u4e0d\u80fd\u6539\u53d8\u5927\u5c0f\u3002\u56e0\u6b64\uff0c\u76f4\u63a5\u5411\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u662f\u4e0d\u53ef\u80fd\u7684\u3002<\/p>\n<p>\u7136\u800c\uff0c\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528ArrayList<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>ArrayList\u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u5217\u8868\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46\u5b83\u5141\u8bb8\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u8981\u4f7f\u7528ArrayList\uff0c\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5bfc\u5165java.util.ArrayList\u5305\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aArrayList\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528add()\u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;();\n        names.add(\"John\");\n        names.add(\"Mary\");\n        names.add(\"Bob\");\n        \n        System.out.println(names); \/\/ \u8f93\u51fa\uff1a[John, Mary, Bob]\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u6bd4\u539f\u59cb\u6570\u7ec4\u5927\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u7136\u540e\uff0c\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>int[] original = {1, 2, 3};\nint[] newArray = new int[original.length + 1];\n\n\nfor (int i = 0; i &lt; original.length; i++) {\n    newArray[i] = original[i];\n}\n\nnewArray[newArray.length - 1] = 4;\n\nSystem.out.println(Arrays.toString(newArray)); \/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u600e\u4e48\u529e\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>\u6570\u7ec4\u5728java\u4e2d\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u6dfb\u52a0\u5143\u7d20\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528arraylist\uff1a\u53ef\u53d8\u957f\u5ea6\u5217\u8868\uff0c\u652f\u6301\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u521b\u5efa\u65b0\u6570\u7ec4\uff1a\u6bd4\u539f\u59cb\u6570\u7ec4\u5927\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u539f\u59cb\u6570\u7ec4\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u518d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002 Java\u4e2d\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20 \u5728Java\u4e2d\uff0c\u6570\u7ec4\u662f\u4e00\u7ec4\u56fa\u5b9a\u957f\u5ea6\u7684\u4e0d\u53d8\u96c6\u5408\uff0c\u4e00\u65e6\u521b\u5efa\u540e\u5c31\u4e0d\u80fd\u6539\u53d8\u5927\u5c0f\u3002\u56e0\u6b64\uff0c\u76f4\u63a5\u5411\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u7136\u800c\uff0c\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6cd5\uff1a 1. \u4f7f\u7528ArrayList \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ArrayList\u662f\u4e00\u4e2a\u53ef\u53d8\u957f\u5ea6\u7684\u5217\u8868\uff0c\u7c7b\u4f3c\u4e8e\u6570\u7ec4\uff0c\u4f46\u5b83\u5141\u8bb8\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u8981\u4f7f\u7528ArrayList\uff0c\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5bfc\u5165java.util.ArrayList\u5305\u3002 \u521b\u5efa\u4e00\u4e2aArrayList\u5bf9\u8c61\u3002 \u4f7f\u7528add()\u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002 \u4f8b\u5982\uff1a import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;(); names.add(&#8220;John&#8221;); names.add(&#8220;Mary&#8221;); names.add(&#8220;Bob&#8221;); System.out.println(names); \/\/ \u8f93\u51fa\uff1a[John, Mary, Bob] } } \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4 \u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u6bd4\u539f\u59cb\u6570\u7ec4\u5927\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u7136\u540e\uff0c\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u7684\u672b\u5c3e\u3002 \u4f8b\u5982\uff1a int[] original = {1, 2, 3}; int[] newArray = new int[original.length [&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-64348","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64348","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=64348"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64348\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}