{"id":65869,"date":"2025-05-03T14:19:57","date_gmt":"2025-05-03T06:19:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65869\/"},"modified":"2025-05-03T14:19:57","modified_gmt":"2025-05-03T06:19:57","slug":"java%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6%e6%9c%aa%e7%9f%a5%e6%80%8e%e4%b9%88%e7%bb%93%e6%9d%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65869\/","title":{"rendered":"java\u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u600e\u4e48\u7ed3\u675f"},"content":{"rendered":"<blockquote><p>\n  \u5bf9\u4e8ejava\u4e2d\u957f\u5ea6\u672a\u77e5\u7684\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u786e\u5b9a\u5176\u7ed3\u675f\uff1a\u4f7f\u7528\u54e8\u5175\u503c\uff0c\u5373\u7279\u6b8a\u503c\u8868\u793a\u6570\u7ec4\u7ed3\u675f\u3002\u4f7f\u7528try-catch\u5757\uff0c\u5f53\u8bbf\u95ee\u8d85\u51fa\u957f\u5ea6\u5143\u7d20\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4f7f\u7528arrays.aslist()\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3alist\uff0c\u904d\u5386\u81f3\u9047\u5230null\u7ed3\u675f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313303529229.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u600e\u4e48\u7ed3\u675f\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u600e\u4e48\u7ed3\u675f\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u65f6\u5982\u4f55\u7ed3\u675f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u5fc5\u987b\u6307\u5b9a\u3002\u4f46\u662f\uff0c\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u957f\u5ea6\u672a\u77e5\u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u786e\u5b9a\u6570\u7ec4\u7684\u7ed3\u675f\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u54e8\u5175\u503c<\/strong><\/p>\n<p>\u54e8\u5175\u503c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u503c\uff0c\u5b83\u8868\u793a\u6570\u7ec4\u7684\u7ed3\u675f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbe\u7f6e\u4e3a -1\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u76f4\u5230\u627e\u5230\u54e8\u5175\u503c\u4e3a\u6b62\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, -1};\nfor (int i = 0; i &lt; arr.length; i++) {\n    if (arr[i] == -1) {\n        break;\n    }\n    \/\/ \u5904\u7406\u6570\u7ec4\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee\u6570\u7ec4\u8d85\u51fa\u5176\u957f\u5ea6\u7684\u5143\u7d20\u65f6\uff0c\u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u5f02\u5e38\u6765\u786e\u5b9a\u6570\u7ec4\u7684\u7ed3\u675f\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\ntry {\n    for (int i = 0; i &lt; arr.length; i++) {\n        \/\/ \u5904\u7406\u6570\u7ec4\u5143\u7d20\n    }\n} catch (ArrayIndexOutOfBoundsException e) {\n    \/\/ \u6570\u7ec4\u5df2\u7ed3\u675f\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.asList()<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Arrays.asList() \u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a List\u3002List \u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u7d22\u5f15\u904d\u5386\u5143\u7d20\uff0c\u76f4\u5230\u9047\u5230 null \u4e3a\u6b62\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nList&lt;Integer&gt; list = Arrays.asList(arr);\nfor (int i = 0; i &lt; list.size(); i++) {\n    if (list.get(i) == null) {\n        break;\n    }\n    \/\/ \u5904\u7406\u6570\u7ec4\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u600e\u4e48\u7ed3\u675f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8ejava\u4e2d\u957f\u5ea6\u672a\u77e5\u7684\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u786e\u5b9a\u5176\u7ed3\u675f\uff1a\u4f7f\u7528\u54e8\u5175\u503c\uff0c\u5373\u7279\u6b8a\u503c\u8868\u793a\u6570\u7ec4\u7ed3\u675f\u3002\u4f7f\u7528try-catch\u5757\uff0c\u5f53\u8bbf\u95ee\u8d85\u51fa\u957f\u5ea6\u5143\u7d20\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4f7f\u7528arrays.aslist()\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3alist\uff0c\u904d\u5386\u81f3\u9047\u5230null\u7ed3\u675f\u3002 Java \u6570\u7ec4\u957f\u5ea6\u672a\u77e5\u65f6\u5982\u4f55\u7ed3\u675f \u5728 Java \u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u5728\u521b\u5efa\u6570\u7ec4\u65f6\u5fc5\u987b\u6307\u5b9a\u3002\u4f46\u662f\uff0c\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u957f\u5ea6\u672a\u77e5\u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u786e\u5b9a\u6570\u7ec4\u7684\u7ed3\u675f\uff1a 1. \u4f7f\u7528\u54e8\u5175\u503c \u54e8\u5175\u503c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u503c\uff0c\u5b83\u8868\u793a\u6570\u7ec4\u7684\u7ed3\u675f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8bbe\u7f6e\u4e3a -1\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u76f4\u5230\u627e\u5230\u54e8\u5175\u503c\u4e3a\u6b62\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, -1}; for (int i = 0; i &lt; arr.length; i++) { if (arr[i] == -1) { break; } \/\/ \u5904\u7406\u6570\u7ec4\u5143\u7d20 } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 try-catch \u5757 \u5982\u679c\u6211\u4eec\u4e0d\u786e\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u3002\u5f53\u6211\u4eec\u8bd5\u56fe\u8bbf\u95ee\u6570\u7ec4\u8d85\u51fa\u5176\u957f\u5ea6\u7684\u5143\u7d20\u65f6\uff0c\u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u5f02\u5e38\u6765\u786e\u5b9a\u6570\u7ec4\u7684\u7ed3\u675f\u3002 int[] arr = {1, 2, 3, [&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-65869","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65869","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=65869"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65869\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}