{"id":37528,"date":"2024-11-26T14:07:47","date_gmt":"2024-11-26T06:07:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37528\/"},"modified":"2024-11-26T14:07:47","modified_gmt":"2024-11-26T06:07:47","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%88%9b%e5%bb%ba%e6%95%b4%e5%9e%8b%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37528\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u521b\u5efa\u6574\u578b\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4\u9700\u8981\u58f0\u660e\u6570\u7ec4\u3001\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3001\u5e76\u53ef\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u521d\u59cb\u5316\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111306543174881.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u521b\u5efa\u6574\u578b\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u521b\u5efa\u6574\u578b\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u58f0\u660e\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>int[] myArray;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u521b\u5efa\u6570\u7ec4\uff1a<\/strong><br \/>\u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>myArray = new int[length];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d length \u662f\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u8868\u793a\u6570\u7ec4\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u521d\u59cb\u5316\u6570\u7ec4\uff08\u53ef\u9009\uff09\uff1a<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u503c\uff1a<\/p>\n<pre>int[] myArray = {1, 2, 3, 4, 5};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 5 \u7684\u6574\u578b\u6570\u7ec4\uff1a<\/p>\n<pre>int[] myArray = new int[5];\nmyArray[0] = 10;\nmyArray[1] = 20;\nmyArray[2] = 30;\nmyArray[3] = 40;\nmyArray[4] = 50;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7\u8fd0\u7b97\u7b26\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1a<\/p>\n<pre>int element = myArray[index];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d index \u662f\u8981\u8bbf\u95ee\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u5927\u5c0f\u5728\u521b\u5efa\u540e\u4e0d\u80fd\u66f4\u6539\u3002<\/li>\n<li>\u6570\u7ec4\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a 0\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a length &#8211; 1\u3002<\/li>\n<li>\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u5bfc\u81f4\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38 (IndexOutOfBoundsException)\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u521b\u5efa\u6574\u578b\u6570\u7ec4\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>\u5728 java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4\u9700\u8981\u58f0\u660e\u6570\u7ec4\u3001\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3001\u5e76\u53ef\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u521d\u59cb\u5316\u6570\u7ec4\u3002 Java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4 \u5728 Java \u4e2d\u521b\u5efa\u6574\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u58f0\u660e\u6570\u7ec4\uff1a int[] myArray; \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u6570\u7ec4\uff1a\u4f7f\u7528 new \u5173\u952e\u5b57\u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b myArray = new int[length]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d length \u662f\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u8868\u793a\u6570\u7ec4\u53ef\u4ee5\u5bb9\u7eb3\u7684\u5143\u7d20\u6570\u91cf\u3002 \u521d\u59cb\u5316\u6570\u7ec4\uff08\u53ef\u9009\uff09\uff1a\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u521d\u59cb\u5316\u5668\u4e3a\u6570\u7ec4\u5143\u7d20\u5206\u914d\u503c\uff1a int[] myArray = {1, 2, 3, 4, 5}; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 5 \u7684\u6574\u578b\u6570\u7ec4\uff1a int[] myArray = new int[5]; myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; myArray[3] [&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-37528","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37528","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=37528"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37528\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}