{"id":37186,"date":"2024-11-26T17:39:51","date_gmt":"2024-11-26T09:39:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37186\/"},"modified":"2024-11-26T17:39:51","modified_gmt":"2024-11-26T09:39:51","slug":"java%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%e8%b6%8a%e7%95%8c%e6%80%8e%e4%b9%88%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37186\/","title":{"rendered":"java\u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u5904\u7406"},"content":{"rendered":"<blockquote><p>\n  java \u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u6642\u3001\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u5bfe\u51e6\u53ef\u80fd\uff1abounds()\u30e1\u30bd\u30c3\u30c9\u3067\u6709\u52b9\u7bc4\u56f2\u3092\u78ba\u8a8d\u3059\u308b\u3002try-catch\u30d6\u30ed\u30c3\u30af\u3067\u4f8b\u5916\u3092\u51e6\u7406\u3059\u308b\u3002\u4e88\u9632\u7b56\u3068\u3057\u3066\u306f\u3001bounds()\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u4e8b\u524d\u78ba\u8a8d\u3001\u8ca0\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u56de\u907f\u3001\u914d\u5217\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u306e\u6d3b\u7528\u3001\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u4f7f\u7528\u306a\u3069\u3092\u8003\u616e\u3059\u308b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302243717665.jpg\" class=\"aligncenter\" title=\"java\u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u5904\u7406\u63d2\u56fe\" alt=\"java\u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u5904\u7406<\/strong><\/p>\n<p>\u5f53\u8bbf\u95ee Java \u5bf9\u8c61\u6570\u7ec4\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u8d8a\u754c\u5f02\u5e38\uff08IndexOutOfBoundsException\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u901a\u5e38\u53d1\u751f\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u5904\u7406\u8d8a\u754c\u5f02\u5e38<\/strong><\/p>\n<p>\u5904\u7406\u8d8a\u754c\u5f02\u5e38\u7684\u5e38\u89c1\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\n<p><strong>bounds() \u65b9\u6cd5\uff1a<\/strong> \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6570\u7ec4\u7684\u6709\u6548\u8303\u56f4\u5185\u3002<\/p>\n<pre>if (index &lt; 0 || index &gt;= array.length) {\n  throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + array.length);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>try-catch \u5757\uff1a<\/strong> \u6355\u83b7\u8d8a\u754c\u5f02\u5e38\u5e76\u63d0\u4f9b\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<pre>try {\n  \/\/ \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\n} catch (IndexOutOfBoundsException e) {\n  \/\/ \u5f02\u5e38\u5904\u7406\u903b\u8f91\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u9884\u9632\u8d8a\u754c\u5f02\u5e38<\/strong><\/p>\n<p>\u9664\u4e86\u5904\u7406\u8d8a\u754c\u5f02\u5e38\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u91c7\u53d6\u63aa\u65bd\u6765\u9884\u9632\u5b83\u4eec\uff1a<\/p>\n<ul>\n<li> <strong>\u4f7f\u7528 bounds() \u65b9\u6cd5\uff1a<\/strong> \u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\u59cb\u7ec8\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u6709\u6548\u3002<\/li>\n<li> <strong>\u907f\u514d\u4f7f\u7528\u8d1f\u7d22\u5f15\uff1a<\/strong>\u8d1f\u7d22\u5f15\u603b\u662f\u65e0\u6548\u7684\u3002<\/li>\n<li> <strong>\u4f7f\u7528\u6570\u7ec4\u5de5\u5177\u7c7b\uff1a<\/strong> Apache Commons Lang \u306a\u3069\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306f\u3001\u914d\u5217\u306e\u64cd\u4f5c\u3092\u5bb9\u6613\u306b\u3059\u308b\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n<li> <strong>\u8003\u8651\u4f7f\u7528\u96c6\u5408\uff1a<\/strong> ArrayList \u306a\u3069\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306f\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3088\u308b\u30a2\u30af\u30bb\u30b9\u3067\u306f\u306a\u304f\u3001\u30a4\u30c6\u30ec\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u305f\u914d\u5217\u306e\u53cd\u5fa9\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] arr = new int[5];\n\ntry {\n    int result = arr[5]; \/\/ \u8d8a\u754c\n} catch (IndexOutOfBoundsException e) {\n    System.out.println(\"Invalid index: \" + e.getMessage());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001arr[5] \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u7bc4\u56f2\u5916\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002 try-catch \u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u3001\u3053\u306e\u30a8\u30e9\u30fc\u3092\u9069\u5207\u306b\u51e6\u7406\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\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 \u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u6642\u3001\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u5bfe\u51e6\u53ef\u80fd\uff1abounds()\u30e1\u30bd\u30c3\u30c9\u3067\u6709\u52b9\u7bc4\u56f2\u3092\u78ba\u8a8d\u3059\u308b\u3002try-catch\u30d6\u30ed\u30c3\u30af\u3067\u4f8b\u5916\u3092\u51e6\u7406\u3059\u308b\u3002\u4e88\u9632\u7b56\u3068\u3057\u3066\u306f\u3001bounds()\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308b\u4e8b\u524d\u78ba\u8a8d\u3001\u8ca0\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u56de\u907f\u3001\u914d\u5217\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u306e\u6d3b\u7528\u3001\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u4f7f\u7528\u306a\u3069\u3092\u8003\u616e\u3059\u308b\u3002 Java \u5bf9\u8c61\u6570\u7ec4\u8d8a\u754c\u5904\u7406 \u5f53\u8bbf\u95ee Java \u5bf9\u8c61\u6570\u7ec4\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u8d8a\u754c\u5f02\u5e38\uff08IndexOutOfBoundsException\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u901a\u5e38\u53d1\u751f\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u3002 \u5904\u7406\u8d8a\u754c\u5f02\u5e38 \u5904\u7406\u8d8a\u754c\u5f02\u5e38\u7684\u5e38\u89c1\u65b9\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b bounds() \u65b9\u6cd5\uff1a \u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6570\u7ec4\u7684\u6709\u6548\u8303\u56f4\u5185\u3002 if (index &lt; 0 || index &gt;= array.length) { throw new IndexOutOfBoundsException(&#8220;Index: &#8221; + index + &#8220;, Size: &#8221; + array.length); } \u767b\u5f55\u540e\u590d\u5236 try-catch \u5757\uff1a \u6355\u83b7\u8d8a\u754c\u5f02\u5e38\u5e76\u63d0\u4f9b\u9002\u5f53\u7684\u5904\u7406\u3002 try { \/\/ \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20 } catch (IndexOutOfBoundsException e) { \/\/ \u5f02\u5e38\u5904\u7406\u903b\u8f91 } \u767b\u5f55\u540e\u590d\u5236 \u9884\u9632\u8d8a\u754c\u5f02\u5e38 \u9664\u4e86\u5904\u7406\u8d8a\u754c\u5f02\u5e38\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u91c7\u53d6\u63aa\u65bd\u6765\u9884\u9632\u5b83\u4eec\uff1a \u4f7f\u7528 [&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-37186","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37186","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=37186"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37186\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}