{"id":65486,"date":"2025-05-03T11:39:51","date_gmt":"2025-05-03T03:39:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65486\/"},"modified":"2025-05-03T11:39:51","modified_gmt":"2025-05-03T03:39:51","slug":"java%e7%b1%bb%e7%b1%bb%e5%9e%8b%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%b5%8b%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65486\/","title":{"rendered":"java\u7c7b\u7c7b\u578b\u6570\u7ec4\u600e\u4e48\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1aclasstype[] array = { new classtype1(), new classtype2(), &#8230; }\u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1aclasstype[] array = new classtype[size]; for (int i = 0; i <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302095653620.jpg\" class=\"aligncenter\" title=\"java\u7c7b\u7c7b\u578b\u6570\u7ec4\u600e\u4e48\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u7c7b\u7c7b\u578b\u6570\u7ec4\u600e\u4e48\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\uff1f<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>ClassType[] array = { new ClassType1(), new ClassType2(), ... };<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1a<\/strong><\/p>\n<pre>ClassType[] array = new ClassType[size];\nfor (int i = 0; i &lt; size; i++) {\n  array[i] = new ClassType();\n  \/\/ \u6216 array[i] = (ClassType) new Object();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>ClassType[] array = new ClassType[size];\nArrays.fill(array, new ClassType());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a Student \u7c7b\u578b\u7684\u6570\u7ec4\nStudent[] students = new Student[3];\n\n\/\/ \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u8d4b\u503c\nstudents[0] = new Student(\"John\", 21);\nstudents[1] = new Student(\"Alice\", 20);\nstudents[2] = new Student(\"Bob\", 22);\n\n\/\/ \u4f7f\u7528\u5faa\u73af\u8d4b\u503c\nstudents = new Student[5];\nfor (int i = 0; i &lt; students.length; i++) {\n  students[i] = new Student(\"Student\" + (i + 1), i + 10);\n}\n\n\/\/ \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5\u8d4b\u503c\nstudents = new Student[10];\nArrays.fill(students, new Student(\"Default Student\", 0));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u7c7b\u7c7b\u578b\u6570\u7ec4\u600e\u4e48\u8d4b\u503c\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>java \u4e2d\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1aclasstype[] array = { new classtype1(), new classtype2(), &#8230; }\u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1aclasstype[] array = new classtype[size]; for (int i = 0; i \u5982\u4f55\u5728 Java \u4e2d\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\uff1f \u65b9\u6cd5\uff1a \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4e3a\u7c7b\u7c7b\u578b\u6570\u7ec4\u8d4b\u503c\uff1a 1. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ClassType[] array = { new ClassType1(), new ClassType2(), &#8230; }; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1a ClassType[] array = new ClassType[size]; for (int i = 0; i [&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-65486","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65486","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=65486"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65486\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}