{"id":66081,"date":"2025-05-03T17:32:27","date_gmt":"2025-05-03T09:32:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66081\/"},"modified":"2025-05-03T17:32:27","modified_gmt":"2025-05-03T09:32:27","slug":"%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8a%a8%e6%80%81%e6%89%a9%e5%ae%b9java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66081\/","title":{"rendered":"\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u52a8\u6001\u6269\u5bb9JAVA"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 arraylist \u53ef\u5b9e\u73b0 java \u4e8c\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u6269\u5bb9\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa arraylist \u7684 arraylist\u3001\u521d\u59cb\u5316\u7b2c\u4e00\u884c\u3001\u6269\u5bb9\u4e00\u884c\u3001\u6269\u5bb9\u4e00\u5217\u3001\u8bbe\u7f6e\u5143\u7d20\u503c\u548c\u83b7\u53d6\u5143\u7d20\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111413391961565.jpg\" class=\"aligncenter\" title=\"\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u52a8\u6001\u6269\u5bb9JAVA\u63d2\u56fe\" alt=\"\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u52a8\u6001\u6269\u5bb9JAVA\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u4e3a\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u52a8\u6001\u6269\u5bb9<\/strong><\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Java \u4e2d\u7684 ArrayList &gt; \u6570\u636e\u7ed3\u6784\u3002 <\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a\u5305\u542b ArrayList \u7684 ArrayList<\/strong><\/p>\n<pre>List&lt;List&lt;Integer&gt;&gt; dynamic2DArray = new ArrayList&lt;&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521d\u59cb\u5316\u7b2c\u4e00\u884c<\/strong><\/p>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u7a7a ArrayList \u8868\u793a\u7b2c\u4e00\u884c\uff1a<\/p>\n<pre>dynamic2DArray.add(new ArrayList&lt;&gt;());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6269\u5bb9\u4e00\u884c<\/strong><\/p>\n<p>\u8981\u6269\u5bb9\u4e00\u884c\uff0c\u8bf7\u521b\u5efa\u53e6\u4e00\u4e2a ArrayList \u5e76\u5c06\u5176\u6dfb\u52a0\u5230 dynamic2DArray \u4e2d\uff1a<\/p>\n<pre>dynamic2DArray.add(new ArrayList&lt;&gt;());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u6269\u5bb9\u4e00\u5217<\/strong><\/p>\n<p>\u8981\u6269\u5bb9\u4e00\u5217\uff0c\u8bf7\u904d\u5386\u6240\u6709\u73b0\u6709\u884c\u5e76\u5411\u6bcf\u884c\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>for (List&lt;Integer&gt; row : dynamic2DArray) {\n    row.add(0);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u8bbe\u7f6e\u5143\u7d20\u503c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 set() \u65b9\u6cd5\u8bbe\u7f6e\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff1a<\/p>\n<pre>dynamic2DArray.get(rowIndex).set(columnIndex, value);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u83b7\u53d6\u5143\u7d20\u503c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 get() \u65b9\u6cd5\u83b7\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff1a<\/p>\n<pre>int value = dynamic2DArray.get(rowIndex).get(columnIndex);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a 2x2 \u7684\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\nList\n  \n   &gt; dynamicArray = new ArrayList&lt;&gt;();\ndynamicArray.add(new ArrayList&lt;&gt;());\ndynamicArray.add(new ArrayList&lt;&gt;());\ndynamic2DArray.get(0).add(1);\ndynamic2DArray.get(0).add(2);\ndynamic2DArray.get(1).add(3);\ndynamic2DArray.get(1).add(4);\n\n\/\/ \u5411\u6570\u7ec4\u4e2d\u6269\u5bb9\u4e00\u884c\ndynamic2DArray.add(new ArrayList&lt;&gt;());\n\n\/\/ \u5411\u6570\u7ec4\u4e2d\u6269\u5bb9\u4e00\u5217\nfor (List&lt;Integer&gt; row : dynamic2DArray) {\n    row.add(0);\n}\n\n\/\/ \u8bbe\u7f6e\u5143\u7d20\u503c\ndynamic2DArray.get(2).set(2, 5);\n\n\/\/ \u83b7\u53d6\u5143\u7d20\u503c\nint value = dynamic2DArray.get(1).get(1);\n  <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e8c\u7ef4\u6570\u7ec4\u600e\u4e48\u52a8\u6001\u6269\u5bb9JAVA\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>\u4f7f\u7528 arraylist \u53ef\u5b9e\u73b0 java \u4e8c\u7ef4\u6570\u7ec4\u7684\u52a8\u6001\u6269\u5bb9\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa arraylist \u7684 arraylist\u3001\u521d\u59cb\u5316\u7b2c\u4e00\u884c\u3001\u6269\u5bb9\u4e00\u884c\u3001\u6269\u5bb9\u4e00\u5217\u3001\u8bbe\u7f6e\u5143\u7d20\u503c\u548c\u83b7\u53d6\u5143\u7d20\u503c\u3002 \u5982\u4f55\u4f7f\u7528 Java \u4e3a\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u52a8\u6001\u6269\u5bb9 \u89e3\u51b3\u65b9\u6848\uff1a \u4f7f\u7528 Java \u4e2d\u7684 ArrayList &gt; \u6570\u636e\u7ed3\u6784\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u521b\u5efa\u4e00\u4e2a\u5305\u542b ArrayList \u7684 ArrayList List&lt;List&lt;Integer&gt;&gt; dynamic2DArray = new ArrayList&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 2. \u521d\u59cb\u5316\u7b2c\u4e00\u884c \u6dfb\u52a0\u4e00\u4e2a\u7a7a ArrayList \u8868\u793a\u7b2c\u4e00\u884c\uff1a dynamic2DArray.add(new ArrayList&lt;&gt;()); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 3. \u6269\u5bb9\u4e00\u884c \u8981\u6269\u5bb9\u4e00\u884c\uff0c\u8bf7\u521b\u5efa\u53e6\u4e00\u4e2a ArrayList \u5e76\u5c06\u5176\u6dfb\u52a0\u5230 dynamic2DArray \u4e2d\uff1a dynamic2DArray.add(new ArrayList&lt;&gt;()); \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 4. \u6269\u5bb9\u4e00\u5217 \u8981\u6269\u5bb9\u4e00\u5217\uff0c\u8bf7\u904d\u5386\u6240\u6709\u73b0\u6709\u884c\u5e76\u5411\u6bcf\u884c\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff1a for [&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-66081","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66081","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=66081"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66081\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}