{"id":38092,"date":"2024-11-26T13:01:00","date_gmt":"2024-11-26T05:01:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38092\/"},"modified":"2024-11-26T13:01:00","modified_gmt":"2024-11-26T05:01:00","slug":"java%e8%bf%b7%e5%ae%ab%e4%b8%ad%e6%95%b0%e7%bb%84%e5%bc%82%e5%b8%b8%e6%80%8e%e4%b9%88%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38092\/","title":{"rendered":"java\u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u600e\u4e48\u5904\u7406"},"content":{"rendered":"<blockquote><p>\n  java \u8ff7\u5bab\u6e38\u620f\u4e2d\u6570\u7ec4\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u5305\u62ec\uff1a\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\uff1b\u4f7f\u7528 try-catch \u5757\uff1b\u4f7f\u7528 java collections api\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111316452990766.jpg\" class=\"aligncenter\" title=\"java\u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u600e\u4e48\u5904\u7406\u63d2\u56fe\" alt=\"java\u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u600e\u4e48\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u7684\u5904\u7406<\/strong><\/p>\n<p>\u5728 Java \u8ff7\u5bab\u6e38\u620f\u4e2d\uff0c\u6570\u7ec4\u5f02\u5e38\u901a\u5e38\u662f\u56e0\u4e3a\u904d\u5386\u6570\u7ec4\u65f6\u8d85\u51fa\u5176\u8fb9\u754c\u800c\u5f15\u8d77\u7684\u3002\u8fd9\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u4f8b\u5982 IndexOutOfBoundsException\u3002\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u6e38\u620f\u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5904\u7406\u6570\u7ec4\u5f02\u5e38\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u6570\u7ec4\u8fb9\u754c\u68c0\u67e5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728\u904d\u5386\u6570\u7ec4\u4e4b\u524d\uff0c\u6700\u597d\u5bf9\u7d22\u5f15\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u5176\u4f4d\u4e8e\u6570\u7ec4\u7684\u6709\u6548\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre>if (index &lt; 0 || index &gt;= array.length) {\n    throw new IndexOutOfBoundsException(\"\u6570\u7ec4\u7d22\u5f15\u8d8a\u754c\uff1a\" + index);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u6765\u6355\u83b7\u7531\u4e8e\u6570\u7ec4\u5f02\u5e38\u800c\u5bfc\u81f4\u7684\u5f02\u5e38\u3002\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6355\u83b7\uff1a<\/p>\n<pre>try {\n    \/\/ \u8bbf\u95ee\u6570\u7ec4\u7684\u4ee3\u7801\n} catch (IndexOutOfBoundsException e) {\n    \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u8bb0\u5f55\u5f02\u5e38\u3001\u901a\u77e5\u7528\u6237\u6216\u91c7\u53d6\u5176\u4ed6\u9002\u5f53\u7684\u63aa\u65bd\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 Java Collections API<\/strong><\/p>\n<p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528 Java Collections API \u4e2d\u7684\u7c7b\uff0c\u4f8b\u5982 List \u548c Map\uff0c\u5b83\u4eec\u63d0\u4f9b\u5185\u7f6e\u7684\u8303\u56f4\u68c0\u67e5\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p><strong>4. \u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u59cb\u7ec8\u4f7f\u7528\u5408\u9002\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6570\u7ec4\u7d22\u5f15\u8d8a\u754c\uff0c\u5e94\u4f7f\u7528 IndexOutOfBoundsException\u3002<\/li>\n<li>\u5728\u5904\u7406\u5f02\u5e38\u65f6\u63d0\u4f9b\u6709\u7528\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u5f02\u5e38\u6d88\u606f\u548c\u4ee3\u7801\u884c\u53f7\u3002<\/li>\n<li>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u9632\u6b62\u6570\u7ec4\u5f02\u5e38\u53d1\u751f\uff0c\u4f8b\u5982\u901a\u8fc7\u9884\u5148\u9a8c\u8bc1\u8f93\u5165\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u600e\u4e48\u5904\u7406\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 \u8ff7\u5bab\u6e38\u620f\u4e2d\u6570\u7ec4\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u5305\u62ec\uff1a\u6570\u7ec4\u8fb9\u754c\u68c0\u67e5\uff1b\u4f7f\u7528 try-catch \u5757\uff1b\u4f7f\u7528 java collections api\u3002 Java \u8ff7\u5bab\u4e2d\u6570\u7ec4\u5f02\u5e38\u7684\u5904\u7406 \u5728 Java \u8ff7\u5bab\u6e38\u620f\u4e2d\uff0c\u6570\u7ec4\u5f02\u5e38\u901a\u5e38\u662f\u56e0\u4e3a\u904d\u5386\u6570\u7ec4\u65f6\u8d85\u51fa\u5176\u8fb9\u754c\u800c\u5f15\u8d77\u7684\u3002\u8fd9\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u4f8b\u5982 IndexOutOfBoundsException\u3002\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u6e38\u620f\u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u81f3\u5173\u91cd\u8981\u3002 \u5904\u7406\u6570\u7ec4\u5f02\u5e38\u7684\u65b9\u6cd5\uff1a 1. \u6570\u7ec4\u8fb9\u754c\u68c0\u67e5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728\u904d\u5386\u6570\u7ec4\u4e4b\u524d\uff0c\u6700\u597d\u5bf9\u7d22\u5f15\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u5176\u4f4d\u4e8e\u6570\u7ec4\u7684\u6709\u6548\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\uff1a if (index &lt; 0 || index &gt;= array.length) { throw new IndexOutOfBoundsException(&#8220;\u6570\u7ec4\u7d22\u5f15\u8d8a\u754c\uff1a&#8221; + index); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 try-catch \u5757 \u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u6765\u6355\u83b7\u7531\u4e8e\u6570\u7ec4\u5f02\u5e38\u800c\u5bfc\u81f4\u7684\u5f02\u5e38\u3002\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6355\u83b7\uff1a try { \/\/ \u8bbf\u95ee\u6570\u7ec4\u7684\u4ee3\u7801 } catch (IndexOutOfBoundsException e) { \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801 } \u767b\u5f55\u540e\u590d\u5236 \u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u8bb0\u5f55\u5f02\u5e38\u3001\u901a\u77e5\u7528\u6237\u6216\u91c7\u53d6\u5176\u4ed6\u9002\u5f53\u7684\u63aa\u65bd\u3002 [&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-38092","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38092","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=38092"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38092\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}