{"id":37991,"date":"2024-11-26T10:32:30","date_gmt":"2024-11-26T02:32:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37991\/"},"modified":"2024-11-26T10:32:30","modified_gmt":"2024-11-26T02:32:30","slug":"java%e6%80%8e%e4%b9%88%e7%bb%99%e5%bc%95%e7%94%a8%e5%9e%8b%e6%95%b0%e7%bb%84%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37991\/","title":{"rendered":"java\u600e\u4e48\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c"},"content":{"rendered":"<blockquote><p>\n  \u7ed9 java \u4e2d\u7684\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528\u7d22\u5f15\u8d4b\u503c\uff1b3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1b4. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u8d4b\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111314452267739.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5f15\u7528\u578b\u6570\u7ec4\u662f\u4e00\u79cd\u6307\u5411\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u5b83\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u76f8\u5bf9\u7b80\u5355\uff0c\u53ea\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5f15\u7528\u578b\u6570\u7ec4\u5e76\u4e3a\u5176\u5206\u914d\u7a7a\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>DataType[] arrayName = new DataType[size];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>DataType\uff1a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/li>\n<li>arrayName\uff1a\u6570\u7ec4\u7684\u540d\u79f0\u3002<\/li>\n<li>size\uff1a\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528\u7d22\u5f15\u8d4b\u503c<\/strong><\/p>\n<p>\u8981\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5bf9\u8c61\u5f15\u7528\u8d4b\u503c\u7ed9\u8be5\u5143\u7d20\uff1a<\/p>\n<pre>arrayName[index] = objectReference;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>index\uff1a\u8981\u8d4b\u503c\u7684\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/li>\n<li>objectReference\uff1a\u8981\u8d4b\u7ed9\u6570\u7ec4\u5143\u7d20\u7684\u5bf9\u8c61\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c<\/strong><\/p>\n<p>\u5982\u679c\u8981\u7ed9\u6574\u4e2a\u6570\u7ec4\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u3002\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u4f7f\u7528 for \u5faa\u73af\u7ed9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\uff1a<\/p>\n<pre>for (int i = 0; i &lt; arrayName.length; i++) {\n  arrayName[i] = objectReference;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u8d4b\u503c<\/strong><\/p>\n<p>Java \u8fd8\u5141\u8bb8\u5728\u6570\u7ec4\u58f0\u660e\u65f6\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u6765\u521d\u59cb\u5316\u6570\u7ec4\u5143\u7d20\u3002\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\uff1a<\/p>\n<pre>DataType[] arrayName = {objectReference1, objectReference2, ..., objectReferenceN};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u6307\u5411 String \u7c7b\u578b\u7684\u6570\u7ec4\nString[] stringArray = new String[3];\n\n\/\/ \u4f7f\u7528\u7d22\u5f15\u7ed9\u6570\u7ec4\u5143\u7d20\u8d4b\u503c\nstringArray[0] = \"Hello\";\nstringArray[1] = \"World\";\nstringArray[2] = \"Java\";\n\n\/\/ \u4f7f\u7528\u5faa\u73af\u7ed9\u6570\u7ec4\u5143\u7d20\u8d4b\u503c\nfor (int i = 0; i &lt; stringArray.length; i++) {\n  stringArray[i] = \"Element \" + (i + 1);\n}\n\n\/\/ \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u7ed9\u6570\u7ec4\u8d4b\u503c\nint[] intArray = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed9 Java \u4e2d\u7684\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\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>\u7ed9 java \u4e2d\u7684\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a1. \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u6570\u7ec4\uff1b2. \u4f7f\u7528\u7d22\u5f15\u8d4b\u503c\uff1b3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c\uff1b4. \u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u8d4b\u503c\u3002 \u5982\u4f55\u5728 Java \u4e2d\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c \u5728 Java \u4e2d\uff0c\u5f15\u7528\u578b\u6570\u7ec4\u662f\u4e00\u79cd\u6307\u5411\u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u5b83\u5b58\u50a8\u7684\u662f\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\u76f8\u5bf9\u7b80\u5355\uff0c\u53ea\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u6570\u7ec4 \u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5f15\u7528\u578b\u6570\u7ec4\u5e76\u4e3a\u5176\u5206\u914d\u7a7a\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b DataType[] arrayName = new DataType[size]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a DataType\uff1a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\u3002 arrayName\uff1a\u6570\u7ec4\u7684\u540d\u79f0\u3002 size\uff1a\u6570\u7ec4\u7684\u5927\u5c0f\u3002 2. \u4f7f\u7528\u7d22\u5f15\u8d4b\u503c \u8981\u7ed9\u5f15\u7528\u578b\u6570\u7ec4\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5bf9\u8c61\u5f15\u7528\u8d4b\u503c\u7ed9\u8be5\u5143\u7d20\uff1a arrayName[index] = objectReference; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a index\uff1a\u8981\u8d4b\u503c\u7684\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\u3002 objectReference\uff1a\u8981\u8d4b\u7ed9\u6570\u7ec4\u5143\u7d20\u7684\u5bf9\u8c61\u5f15\u7528\u3002 3. \u4f7f\u7528\u5faa\u73af\u8d4b\u503c \u5982\u679c\u8981\u7ed9\u6574\u4e2a\u6570\u7ec4\u8d4b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u3002\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u4f7f\u7528 for \u5faa\u73af\u7ed9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\uff1a for (int i = 0; i &lt; arrayName.length; i++) { arrayName[i] = objectReference; [&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-37991","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37991","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=37991"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37991\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}