{"id":37251,"date":"2024-11-26T17:28:25","date_gmt":"2024-11-26T09:28:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37251\/"},"modified":"2024-11-26T17:28:25","modified_gmt":"2024-11-26T09:28:25","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%8408%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37251\/","title":{"rendered":"java\u4e2d\u6570\u7ec408\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<blockquote><p>\n  \u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff08indexoutofboundsexception\uff09\u53d1\u751f\u5728\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u8fb9\u754c\uff08\u8d77\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\uff09\u7684\u5143\u7d20\u65f6\u3002\u89e3\u51b3\u65b9\u6cd5\u5305\u62ec\uff1a\u9a8c\u8bc1\u6570\u7ec4\u7d22\u5f15\u7684\u6709\u6548\u6027\u3002\u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93\u3002\u4f7f\u7528 java 9 \u5f15\u5165\u7684\u8fb9\u754c\u68c0\u67e5\u7279\u6027\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111303033268689.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u6570\u7ec408\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" alt=\"java\u4e2d\u6570\u7ec408\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff1aIndexOutOfBoundsException<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff1f<\/strong><\/p>\n<p>\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff08IndexOutOfBoundsException\uff09\u662f\u5728 Java \u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\u53ef\u80fd\u9047\u5230\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u5b83\u53d1\u751f\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6570\u7ec4\u4e2d\u8d85\u51fa\u5176\u8fb9\u754c\uff08\u8d77\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\uff09\u7684\u5143\u7d20\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u89e3\u51b3\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li> <strong>\u9a8c\u8bc1\u6570\u7ec4\u7d22\u5f15\uff1a<\/strong>\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6709\u6548\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u6216\u6570\u7ec4\u7684 length \u5c5e\u6027\u6765\u68c0\u67e5\u7d22\u5f15\u7684\u6709\u6548\u6027\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 Guava \u6216 Apache Commons Lang\uff0c\u6765\u65b9\u4fbf\u5730\u68c0\u67e5\u6570\u7ec4\u8fb9\u754c\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u8d85\u51fa\u8303\u56f4\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u8fb9\u754c\u68c0\u67e5\u7279\u6027\uff1a<\/strong>Java 9 \u5f15\u5165\u4e86\u4e00\u9879\u65b0\u7684\u7279\u6027\uff0c\u5141\u8bb8\u6570\u7ec4\u58f0\u660e\u4e00\u4e2a\u8fb9\u754c\u68c0\u67e5\u7b56\u7565\u3002\u901a\u8fc7\u4f7f\u7528\u6b64\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u7684\u5143\u7d20\u65f6\u89e6\u53d1\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = {1, 2, 3};\n\n\/\/ \u9a8c\u8bc1\u7d22\u5f15\u7684\u6709\u6548\u6027\nif (index &lt; 0 || index &gt;= arr.length) {\n    throw new IndexOutOfBoundsException(\"\u6570\u7ec4\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\");\n}\n\n\/\/ \u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93\nimport com.google.common.base.Preconditions;\n\n\/\/ \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u8d85\u51fa\u8303\u56f4\nPreconditions.checkElementIndex(index, arr.length);\n\n\/\/ \u4f7f\u7528\u8fb9\u754c\u68c0\u67e5\u7279\u6027\nimport java.lang.invoke.MethodHandles.Lookup;\n\n\/\/ \u83b7\u53d6\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u7b56\u7565\nLookup lookup = MethodHandles.lookup();\nint[] boundsCheckedArr = (int[]) lookup.newArray(arr.getClass(), arr.length);\n\n\/\/ \u8bbf\u95ee\u5e26\u6709\u8fb9\u754c\u68c0\u67e5\u7684\u5143\u7d20\nint element = boundsCheckedArr[index];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u91c7\u53d6\u8fd9\u4e9b\u63aa\u65bd\uff0c\u53ef\u4ee5\u907f\u514d\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u5e76\u786e\u4fdd\u6570\u7ec4\u7684\u4f7f\u7528\u5b89\u5168\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u6570\u7ec408\u600e\u4e48\u89e3\u51b3\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>\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff08indexoutofboundsexception\uff09\u53d1\u751f\u5728\u8bbf\u95ee\u8d85\u51fa\u6570\u7ec4\u8fb9\u754c\uff08\u8d77\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\uff09\u7684\u5143\u7d20\u65f6\u3002\u89e3\u51b3\u65b9\u6cd5\u5305\u62ec\uff1a\u9a8c\u8bc1\u6570\u7ec4\u7d22\u5f15\u7684\u6709\u6548\u6027\u3002\u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93\u3002\u4f7f\u7528 java 9 \u5f15\u5165\u7684\u8fb9\u754c\u68c0\u67e5\u7279\u6027\u3002 Java \u4e2d\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff1aIndexOutOfBoundsException \u4ec0\u4e48\u662f\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff1f \u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff08IndexOutOfBoundsException\uff09\u662f\u5728 Java \u4e2d\u4f7f\u7528\u6570\u7ec4\u65f6\u53ef\u80fd\u9047\u5230\u7684\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u5b83\u53d1\u751f\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6570\u7ec4\u4e2d\u8d85\u51fa\u5176\u8fb9\u754c\uff08\u8d77\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\uff09\u7684\u5143\u7d20\u65f6\u3002 \u5982\u4f55\u89e3\u51b3\uff1f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89e3\u51b3\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a \u9a8c\u8bc1\u6570\u7ec4\u7d22\u5f15\uff1a\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6709\u6548\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u4f7f\u7528 if \u8bed\u53e5\u6216\u6570\u7ec4\u7684 length \u5c5e\u6027\u6765\u68c0\u67e5\u7d22\u5f15\u7684\u6709\u6548\u6027\u3002 \u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93\uff1a\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 Guava \u6216 Apache Commons Lang\uff0c\u6765\u65b9\u4fbf\u5730\u68c0\u67e5\u6570\u7ec4\u8fb9\u754c\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u8d85\u51fa\u8303\u56f4\u3002 \u4f7f\u7528\u8fb9\u754c\u68c0\u67e5\u7279\u6027\uff1aJava 9 \u5f15\u5165\u4e86\u4e00\u9879\u65b0\u7684\u7279\u6027\uff0c\u5141\u8bb8\u6570\u7ec4\u58f0\u660e\u4e00\u4e2a\u8fb9\u754c\u68c0\u67e5\u7b56\u7565\u3002\u901a\u8fc7\u4f7f\u7528\u6b64\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u7684\u5143\u7d20\u65f6\u89e6\u53d1\u5f02\u5e38\u3002 \u793a\u4f8b\uff1a int[] arr = {1, 2, 3}; \/\/ \u9a8c\u8bc1\u7d22\u5f15\u7684\u6709\u6548\u6027 if (index &lt; 0 || index &gt;= arr.length) { throw new IndexOutOfBoundsException(&#8220;\u6570\u7ec4\u7d22\u5f15\u8d85\u51fa\u8303\u56f4&#8221;); } \/\/ \u4f7f\u7528\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\u5e93 import com.google.common.base.Preconditions; \/\/ \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u8d85\u51fa\u8303\u56f4 [&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-37251","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37251","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=37251"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37251\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}