{"id":1195,"date":"2022-10-26T23:00:20","date_gmt":"2022-10-26T15:00:20","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1195"},"modified":"2022-11-27T12:39:48","modified_gmt":"2022-11-27T04:39:48","slug":"%e9%9b%86%e5%90%88-%e4%bd%bf%e7%94%a8iterator%e9%81%8d%e5%8e%86collection%e3%80%81%e8%bf%ad%e4%bb%a3%e5%99%a8iterator%e7%9a%84%e6%89%a7%e8%a1%8c%e5%8e%9f%e7%90%86%e3%80%81iterator%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1195","title":{"rendered":"\u96c6\u5408-\u4f7f\u7528Iterator\u904d\u5386Collection\u3001\u8fed\u4ee3\u5668Iterator\u7684\u6267\u884c\u539f\u7406\u3001Iterator\u904d\u5386\u96c6\u5408\u7684\u4e24\u79cd\u9519\u8bef\u5199\u6cd5\u3001Iterator\u8fed\u4ee3\u5668remove()\u7684\u4f7f\u7528"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P523;\r\n\r\nimport org.junit.jupiter.api.Test;\r\nimport top.qaqaq.java.P520.Person;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.Collection;\r\nimport java.util.Iterator;\r\n\r\n\/**\r\n * \u96c6\u5408\u5143\u7d20\u7684\u904d\u5386\u64cd\u4f5c\uff0c\u4f7f\u7528\u8fed\u4ee3\u5668Iterator\u63a5\u53e3\r\n *  1. \u5185\u90e8\u7684\u65b9\u6cd5\uff1ahasNext() \u548c next()\r\n *  2. \u96c6\u5408\u5bf9\u8c61\u6bcf\u6b21\u8c03\u7528iterator()\u65b9\u6cd5\u90fd\u5f97\u5230\u4e00\u4e2a\u5168\u65b0\u7684\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\r\n *  \u9ed8\u8ba4\u6e38\u6807\u90fd\u5728\u96c6\u5408\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e4b\u524d\u3002\r\n *  3. \u5185\u90e8\u5b9a\u4e49\u4e86remove(),\u53ef\u4ee5\u5728\u904d\u5386\u7684\u65f6\u5019\uff0c\u5220\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002\u6b64\u65b9\u6cd5\u4e0d\u540c\u4e8e\u96c6\u5408\u76f4\u63a5\u8c03\u7528remove()\r\n * @author RichieZhang\r\n * @create 2022-10-26 \u4e0a\u5348 11:56\r\n *\/\r\npublic class IteratorTest {\r\n\r\n    @Test\r\n    public void test1(){\r\n        Collection coll = new ArrayList();\r\n        coll.add(123);\r\n        coll.add(456);\r\n        coll.add(new Person(\"Jerry\", 20));\r\n        coll.add(new String(\"Tom\"));\r\n        coll.add(false);\r\n\r\n        Iterator iterator = coll.iterator();\r\n        \/\/\u65b9\u5f0f\u4e00\uff1a\r\n\/\/        System.out.println(iterator.next());\r\n\/\/        System.out.println(iterator.next());\r\n\/\/        System.out.println(iterator.next());\r\n\/\/        System.out.println(iterator.next());\r\n\/\/        System.out.println(iterator.next());\r\n\/\/        \/\/\u62a5\u5f02\u5e38\uff1aNoSuchElementException\r\n\/\/        System.out.println(iterator.next());\r\n\r\n        \/\/\u65b9\u5f0f\u4e8c\uff1a\u4e0d\u63a8\u8350\r\n\/\/        for (int i = 0; i &lt; coll.size(); i++) {\r\n\/\/            System.out.println(iterator.next());\r\n\/\/        }\r\n\r\n        \/\/\u65b9\u5f0f\u4e09\uff1a\u63a8\u8350\r\n        \/\/hasNext():\u5224\u65ad\u662f\u5426\u8fd8\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20\r\n        while (iterator.hasNext()){\r\n            \/\/next(): \u2460 \u6307\u9488\u4e0b\u79fb \u2461 \u5c06\u4e0b\u79fb\u4ee5\u540e\u96c6\u5408\u4f4d\u7f6e\u4e0a\u7684\u5143\u7d20\u8fd4\u56de\r\n            System.out.println(iterator.next());\r\n        }\r\n    }\r\n\r\n    @Test\r\n    public void test2(){\r\n        Collection coll = new ArrayList();\r\n        coll.add(123);\r\n        coll.add(456);\r\n        coll.add(new Person(\"Jerry\", 20));\r\n        coll.add(new String(\"Tom\"));\r\n        coll.add(false);\r\n\r\n        \/\/\u9519\u8bef\u65b9\u5f0f\u4e00\uff1a\r\n\/\/        Iterator iterator = coll.iterator();\r\n\/\/        while (iterator.next() != null){\r\n\/\/            System.out.println(iterator.next());\r\n\/\/        }\r\n\r\n        \/\/\u9519\u8bef\u65b9\u5f0f\u4e8c\uff1a\r\n        \/\/\u96c6\u5408\u5bf9\u8c61\u6bcf\u6b21\u8c03\u7528iterator()\u65b9\u6cd5\u90fd\u5f97\u5230\u4e00\u4e2a\u5168\u65b0\u7684\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\u9ed8\u8ba4\u6e38\u6807\u90fd\u5728\u96c6\u5408\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e4b\u524d\u3002\r\n        while (coll.iterator().hasNext()){\r\n            System.out.println(coll.iterator().next());\r\n        }\r\n    }\r\n\r\n    \/\/\u6d4b\u8bd5Iterator\u4e2d\u7684remove()\r\n    \/\/\u5982\u679c\u8fd8\u672a\u8c03\u7528next()\u6216\u5728\u4e0a\u4e00\u6b21\u8c03\u7528 next \u65b9\u6cd5\u4e4b\u540e\u5df2\u7ecf\u8c03\u7528\u4e86 remove \u65b9\u6cd5\uff0c\r\n    \/\/\u518d\u8c03\u7528remove\u90fd\u4f1a\u62a5IllegalStateException\u3002\r\n    @Test\r\n    public void test3(){\r\n        Collection coll = new ArrayList();\r\n        coll.add(123);\r\n        coll.add(456);\r\n        coll.add(new Person(\"Jerry\", 20));\r\n        coll.add(new String(\"Tom\"));\r\n        coll.add(false);\r\n\r\n        \/\/\u5220\u9664\u96c6\u5408\u4e2d\"Tom\"\r\n        Iterator iterator = coll.iterator();\r\n        while (iterator.hasNext()){\r\n\/\/            iterator.remove();\r\n            Object obj = iterator.next();\r\n            if(\"Tom\".equals(obj)){\r\n                iterator.remove();\r\n\/\/                iterator.remove();\r\n            }\r\n        }\r\n\r\n        \/\/\u904d\u5386\u7ed3\u5408\r\n        iterator = coll.iterator();\r\n        while (iterator.hasNext()){\r\n            System.out.println(iterator.next());\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1195","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1195"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1195"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1196,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1195\/revisions\/1196"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}