{"id":37409,"date":"2024-11-26T12:51:09","date_gmt":"2024-11-26T04:51:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37409\/"},"modified":"2024-11-26T12:51:09","modified_gmt":"2024-11-26T04:51:09","slug":"java%e6%95%b0%e7%bb%84%e8%b6%8a%e7%95%8c%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\/37409\/","title":{"rendered":"java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u600e\u4e48\u5904\u7406"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5904\u7406\uff1a1. \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\uff1b2. \u4f7f\u7528 try-catch \u5757\uff1b3. \u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff1b4. \u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6846\u67b6\u3002\u9519\u8bef\u5904\u7406\u7b56\u7565\u5305\u62ec\uff1a\u629b\u51fa\u5f02\u5e38\u3001\u8fd4\u56de\u9519\u8bef\u503c\u3001\u8bb0\u5f55\u9519\u8bef\u3002\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u68c0\u67e5\u7d22\u5f15\u3001\u8c03\u6574\u5927\u5c0f\u3001\u7b80\u5316\u5f02\u5e38\u5904\u7406\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111305215110929.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u600e\u4e48\u5904\u7406\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u600e\u4e48\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>Java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u5982\u4f55\u5904\u7406\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong>Java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5904\u7406\uff1a<\/p>\n<p><strong>1. \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u6709\u6548\u3002<\/li>\n<li>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5\u7d22\u5f15\uff1a<\/p>\n<pre>if (index &lt; 0 || index &gt;= array.length) {\n  throw new ArrayIndexOutOfBoundsException();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u65e0\u6cd5\u68c0\u67e5\u7d22\u5f15\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\u4f7f\u7528 try-catch \u5757\u6765\u6355\u83b7\u8d8a\u754c\u5f02\u5e38\u3002<\/li>\n<li>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n  int element = array[index];\n} catch (ArrayIndexOutOfBoundsException e) {\n  \/\/ \u5904\u7406\u8d8a\u754c\u5f02\u5e38\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>3. \u8c03\u6574\u6570\u7ec4\u5927\u5c0f<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u6570\u7ec4\u9700\u8981\u5b58\u50a8\u66f4\u591a\u5143\u7d20\uff0c\u53ef\u4ee5\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 Arrays.copyOf() \u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u5927\u5c0f\u5927\u4e8e\u73b0\u6709\u6570\u7ec4\u3002<\/li>\n<li>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>int[] newArray = Arrays.copyOf(array, array.length + 10);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>4. \u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6846\u67b6<\/strong><\/p>\n<ul>\n<li>\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6846\u67b6\uff08\u5982 Apache Commons Lang\uff09\u6765\u7b80\u5316\u5f02\u5e38\u5904\u7406\u3002<\/li>\n<li>\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u5b9e\u7528\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u68c0\u67e5\u7d22\u5f15\u548c\u6355\u83b7\u8d8a\u754c\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>\u5728\u5904\u7406\u8d8a\u754c\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u9519\u8bef\u5904\u7406\u7b56\u7565\uff1a<\/p>\n<ul>\n<li> <strong>\u629b\u51fa\u5f02\u5e38\uff1a<\/strong>\u5c06\u5f02\u5e38\u4f20\u64ad\u5230\u8c03\u7528\u4ee3\u7801\uff0c\u4ee5\u4fbf\u66f4\u9ad8\u5c42\u7ea7\u4ee3\u7801\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<li> <strong>\u8fd4\u56de\u9519\u8bef\u503c\uff1a<\/strong>\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u503c\uff08\u4f8b\u5982 -1\uff09\u6765\u6307\u793a\u8d8a\u754c\u9519\u8bef\u3002<\/li>\n<li> <strong>\u8bb0\u5f55\u9519\u8bef\uff1a<\/strong>\u5c06\u8d8a\u754c\u5f02\u5e38\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/li>\n<\/ul>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u59cb\u7ec8\u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\u3002<\/li>\n<li>\u6839\u636e\u9700\u8981\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u3002<\/li>\n<li>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6846\u67b6\u6216\u5b9e\u7528\u65b9\u6cd5\u7b80\u5316\u5f02\u5e38\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u8d8a\u754c\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\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5904\u7406\uff1a1. \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15\uff1b2. \u4f7f\u7528 try-catch \u5757\uff1b3. \u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff1b4. \u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6846\u67b6\u3002\u9519\u8bef\u5904\u7406\u7b56\u7565\u5305\u62ec\uff1a\u629b\u51fa\u5f02\u5e38\u3001\u8fd4\u56de\u9519\u8bef\u503c\u3001\u8bb0\u5f55\u9519\u8bef\u3002\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u68c0\u67e5\u7d22\u5f15\u3001\u8c03\u6574\u5927\u5c0f\u3001\u7b80\u5316\u5f02\u5e38\u5904\u7406\u3002 Java\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u5904\u7406 \u95ee\u9898\uff1aJava\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u5982\u4f55\u5904\u7406\uff1f \u56de\u7b54\uff1aJava\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5904\u7406\uff1a 1. \u68c0\u67e5\u6570\u7ec4\u7d22\u5f15 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u6709\u6548\u3002 \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5\u7d22\u5f15\uff1a if (index &lt; 0 || index &gt;= array.length) { throw new ArrayIndexOutOfBoundsException(); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 try-catch \u5757 \u5982\u679c\u65e0\u6cd5\u68c0\u67e5\u7d22\u5f15\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\u4f7f\u7528 try-catch \u5757\u6765\u6355\u83b7\u8d8a\u754c\u5f02\u5e38\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a try { int element = array[index]; } catch (ArrayIndexOutOfBoundsException e) { \/\/ \u5904\u7406\u8d8a\u754c\u5f02\u5e38 } \u767b\u5f55\u540e\u590d\u5236 3. \u8c03\u6574\u6570\u7ec4\u5927\u5c0f \u5982\u679c\u6570\u7ec4\u9700\u8981\u5b58\u50a8\u66f4\u591a\u5143\u7d20\uff0c\u53ef\u4ee5\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\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-37409","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37409","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=37409"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37409\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}