{"id":36843,"date":"2024-11-26T08:26:12","date_gmt":"2024-11-26T00:26:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36843\/"},"modified":"2024-11-26T08:26:12","modified_gmt":"2024-11-26T00:26:12","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8f%96%e5%80%92%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36843\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u53d6\u5012\u6570"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5012\u6570\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u8d1f\u7d22\u5f15\uff1aarray[-index]\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\uff1aarray[array.length &#8211; index]\u4f7f\u7528\u4e0b\u6807\u9012\u51cf\uff1aarray[&#8211;index]\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111220160926244.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u53d6\u5012\u6570\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u53d6\u5012\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u6570\u7ec4\u83b7\u53d6\u5012\u6570\u5143\u7d20<\/strong><\/p>\n<p>Java\u6570\u7ec4\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5143\u7d20\u6309\u7d22\u5f15\u503c\u987a\u5e8f\u5b58\u50a8\u3002\u8981\u83b7\u53d6\u6570\u7ec4\u7684\u5012\u6570\u5143\u7d20\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u8d1f\u7d22\u5f15<\/strong><\/p>\n<ul>\n<li>\u901a\u8fc7\u8d1f\u7d22\u5f15\u53ef\u4ee5\u4ece\u6570\u7ec4\u672b\u5c3e\u5411\u6570\u7ec4\u5f00\u5934\u8bbf\u95ee\u5143\u7d20\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aarray[ -index ]<\/li>\n<li>\u5176\u4e2d\uff0cindex \u4e3a\u8981\u83b7\u53d6\u5143\u7d20\u7684\u5012\u6570\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nSystem.out.println(numbers[-1]); \/\/ \u8f93\u51fa\uff1a5\uff08\u5012\u6570\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6570\u7ec4\u957f\u5ea6<\/strong><\/p>\n<ul>\n<li>Java\u6570\u7ec4\u7684\u957f\u5ea6\u4f1a\u81ea\u52a8\u7ef4\u62a4\uff0c\u53ef\u4ee5\u901a\u8fc7 length \u5c5e\u6027\u83b7\u53d6\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aarray[ array.length &#8211; index ]<\/li>\n<li>\u5176\u4e2d\uff0cindex \u4e3a\u8981\u83b7\u53d6\u5143\u7d20\u7684\u5012\u6570\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nSystem.out.println(numbers[numbers.length - 1]); \/\/ \u8f93\u51fa\uff1a5\uff08\u5012\u6570\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u4e0b\u6807\u9012\u51cf<\/strong><\/p>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7\u4e0b\u6807\u9012\u51cf\u6765\u83b7\u53d6\u5012\u6570\u5143\u7d20\u3002<\/li>\n<li>\u8bed\u6cd5\uff1aarray[ &#8211;index ]<\/li>\n<li>\u5176\u4e2d\uff0cindex \u4e3a\u8981\u83b7\u53d6\u5143\u7d20\u7684\u5012\u6570\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5};\nint index = numbers.length;\nSystem.out.println(numbers[--index]); \/\/ \u8f93\u51fa\uff1a5\uff08\u5012\u6570\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u7d22\u5f15\u4e0d\u80fd\u8d85\u51fa\u6570\u7ec4\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002<\/li>\n<li>\u867d\u7136\u8fd9\u4e09\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u83b7\u53d6\u5012\u6570\u5143\u7d20\uff0c\u4f46\u4f7f\u7528\u8d1f\u7d22\u5f15\u6700\u4e3a\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u53d6\u5012\u6570\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>java \u4e2d\u83b7\u53d6\u6570\u7ec4\u5012\u6570\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u8d1f\u7d22\u5f15\uff1aarray[-index]\u4f7f\u7528\u6570\u7ec4\u957f\u5ea6\uff1aarray[array.length &#8211; index]\u4f7f\u7528\u4e0b\u6807\u9012\u51cf\uff1aarray[&#8211;index] Java\u6570\u7ec4\u83b7\u53d6\u5012\u6570\u5143\u7d20 Java\u6570\u7ec4\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5143\u7d20\u6309\u7d22\u5f15\u503c\u987a\u5e8f\u5b58\u50a8\u3002\u8981\u83b7\u53d6\u6570\u7ec4\u7684\u5012\u6570\u5143\u7d20\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u8d1f\u7d22\u5f15 \u901a\u8fc7\u8d1f\u7d22\u5f15\u53ef\u4ee5\u4ece\u6570\u7ec4\u672b\u5c3e\u5411\u6570\u7ec4\u5f00\u5934\u8bbf\u95ee\u5143\u7d20\u3002 \u8bed\u6cd5\uff1aarray[ -index ] \u5176\u4e2d\uff0cindex \u4e3a\u8981\u83b7\u53d6\u5143\u7d20\u7684\u5012\u6570\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002 \u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers[-1]); \/\/ \u8f93\u51fa\uff1a5\uff08\u5012\u6570\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6570\u7ec4\u957f\u5ea6 Java\u6570\u7ec4\u7684\u957f\u5ea6\u4f1a\u81ea\u52a8\u7ef4\u62a4\uff0c\u53ef\u4ee5\u901a\u8fc7 length \u5c5e\u6027\u83b7\u53d6\u3002 \u8bed\u6cd5\uff1aarray[ array.length &#8211; index ] \u5176\u4e2d\uff0cindex \u4e3a\u8981\u83b7\u53d6\u5143\u7d20\u7684\u5012\u6570\u4f4d\u7f6e\uff08\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff09\u3002 \u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers[numbers.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-36843","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36843","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=36843"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36843\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}