{"id":202763,"date":"2025-05-22T11:30:57","date_gmt":"2025-05-22T03:30:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202763\/"},"modified":"2025-05-22T11:30:57","modified_gmt":"2025-05-22T03:30:57","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e9%9b%86%e5%90%88%e6%a1%86%e6%9e%b6%e4%b8%ad%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202763\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p>\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u7279\u5b9a\u5f02\u5e38\u5e76\u6267\u884c\u76f8\u5e94\u4ee3\u7801\u3002\u5bf9\u4e8e Traversable \u96c6\u5408\uff0c\u4f7f\u7528 Iterable \u63a5\u53e3\u7684 for-each \u5faa\u73af\u5904\u7406\u5f02\u5e38\uff0c\u5982\u679c\u9047\u5230\u5f02\u5e38\uff0c\u8fed\u4ee3\u5c06\u7ec8\u6b62\u5e76\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff1afor (Object element : collection) { \/\/ \u96c6\u5408\u64cd\u4f5c\u4ee3\u7801 } catch (ConcurrentModificationException e) { \/\/ \u5904\u7406\u5e76\u53d1\u4fee\u6539\u5f02\u5e38 }<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729939541671cc855dee70.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>Java \u96c6\u5408\u6846\u67b6\u662f\u5904\u7406\u548c\u7ba1\u7406\u6570\u636e\u7ed3\u6784\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4f46\u662f\uff0c\u5728\u4f7f\u7528\u96c6\u5408\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<p><strong>\u5e38\u89c1\u5f02\u5e38<\/strong><\/p>\n<ul>\n<li><strong>IndexOutOfBoundsException\uff1a<\/strong>\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u8d85\u51fa\u96c6\u5408\u8303\u56f4\u7684\u5143\u7d20\u65f6\u629b\u51fa\u3002<\/li>\n<li><strong>NullPointerException\uff1a<\/strong>\u5f53\u5c1d\u8bd5\u4f7f\u7528\u4e00\u4e2a\u672a\u7ecf\u521d\u59cb\u5316\u7684\u96c6\u5408\u6216\u96c6\u5408\u5143\u7d20\u65f6\u629b\u51fa\u3002<\/li>\n<li><strong>ConcurrentModificationException\uff1a<\/strong>\u5f53\u4e00\u4e2a\u96c6\u5408\u6b63\u5728\u88ab\u591a\u4e2a\u7ebf\u7a0b\u4fee\u6539\u65f6\u629b\u51fa\u3002<\/li>\n<\/ul>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406 Java \u96c6\u5408\u6846\u67b6\u4e2d\u7684\u5f02\u5e38\uff1a<\/p>\n<p><strong>1. try-catch \u5757<\/strong><\/p>\n<p>\u4f7f\u7528 try-catch \u5757\u53ef\u4ee5\u6355\u83b7\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>try {\n    \/\/ \u96c6\u5408\u64cd\u4f5c\u4ee3\u7801\n} catch (IndexOutOfBoundsException e) {\n    \/\/ \u5904\u7406\u8d85\u51fa\u8303\u56f4\u5f02\u5e38\n} catch (NullPointerException e) {\n    \/\/ \u5904\u7406\u7a7a\u6307\u9488\u5f02\u5e38\n}<\/pre>\n<p><strong>2. Iterable \u63a5\u53e3<\/strong><\/p>\n<p>\u5bf9\u4e8e Traversable \u96c6\u5408\uff08\u5982 List \u548c Set\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 Iterable \u63a5\u53e3\u7684 for-each \u5faa\u73af\u6765\u5904\u7406\u5f02\u5e38\u3002\u5982\u679c\u9047\u5230\u5f02\u5e38\uff0c\u8fed\u4ee3\u5c06\u7ec8\u6b62\uff0c\u5e76\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>for (Object element : collection) {\n    \/\/ \u96c6\u5408\u64cd\u4f5c\u4ee3\u7801\n} catch (ConcurrentModificationException e) {\n    \/\/ \u5904\u7406\u5e76\u53d1\u4fee\u6539\u5f02\u5e38\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u904d\u5386\u4e00\u4e2a List \u5e76\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>List&lt;String&gt; list = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; list.size(); i++) {\n    System.out.println(list.get(i));\n}<\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u5904\u7406\u5f02\u5e38\uff0c\u5982\u679c list \u4e3a\u7a7a\u6216\u5305\u542b\u4e0d\u8db3 i \u4e2a\u5143\u7d20\uff0c\u5219\u8fd9\u6bb5\u4ee3\u7801\u5c06\u629b\u51fa IndexOutOfBoundsException\u3002\u8981\u5904\u7406\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\uff1a<\/p>\n<pre>try {\n    for (int i = 0; i &lt; list.size(); i++) {\n        System.out.println(list.get(i));\n    }\n} catch (IndexOutOfBoundsException e) {\n    System.out.println(\"\u627e\u4e0d\u5230\u5143\u7d20\uff1a\" + i);\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c list \u4e3a\u7a7a\u6216\u5305\u542b\u4e0d\u8db3 i \u4e2a\u5143\u7d20\uff0c\u5c06\u6253\u5370\u4e00\u6761\u9519\u8bef\u6d88\u606f\uff0c\u800c\u4ee3\u7801\u5c06\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406\u5f02\u5e38\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5904\u7406&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202763","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202763"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202763\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}