{"id":650,"date":"2015-05-13T16:34:02","date_gmt":"2015-05-13T08:34:02","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=650"},"modified":"2026-08-18T12:09:33","modified_gmt":"2026-08-18T04:09:33","slug":"%e8%bf%ad%e4%bb%a3%e5%99%a8iterator","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=650","title":{"rendered":"\u8fed\u4ee3\u5668(Iterator)"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u4efb\u4f55\u5bb9\u5668\u90fd\u5fc5\u987b\u80fd\u628a\u4e1c\u897f\u653e\u8fdb\u53bb\u3001\u518d\u53d6\u51fa\u6765\u3002\u5bf9 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ArrayList<\/code>\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">add()<\/code> \u8d1f\u8d23\u63d2\u5165\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">get()<\/code> \u662f\u53d6\u51fa\u65b9\u5f0f\u4e4b\u4e00\uff0c\u8fd8\u53ef\u4ee5\u6309\u4e0b\u6807\u968f\u673a\u8bbf\u95ee\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u82e5\u7ad9\u9ad8\u4e00\u5c42\u5c31\u4f1a\u53d1\u73b0\u7f3a\u70b9\uff1a\u4f7f\u7528\u5bb9\u5668\u5fc5\u987b\u77e5\u9053\u5143\u7d20\u7684\u786e\u5207\u7c7b\u578b\u548c\u5bb9\u5668\u79cd\u7c7b\u3002\u539f\u6765\u662f ArrayList\uff0c\u540e\u6765\u60f3\u6362\u6210 Set\uff0c\u6216\u8981\u5199\u4e0d\u5173\u5fc3\u5bb9\u5668\u7c7b\u578b\u7684\u901a\u7528\u4ee3\u7801\uff0c\u96be\u9053\u6574\u6bb5\u91cd\u5199\uff1f\u8fd9\u5c31\u662f<strong style=\"color: #4870ac;\">\u8fed\u4ee3\u5668\uff08Iterator\uff09<\/strong>\u8981\u89e3\u51b3\u7684\u95ee\u9898\u3002<\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u4e3a\u4ec0\u4e48 Collection \u6ca1\u6709 get()<\/h2>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Collection<\/code> \u4e0d\u63d0\u4f9b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">get()<\/code>\u3002\u8981\u904d\u5386\u5176\u4e2d\u5143\u7d20\uff0c\u5fc5\u987b\u7528 Iterator\u3002\u8fed\u4ee3\u5668\u672c\u8eab\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e13\u804c\u904d\u5386\u5e76\u9009\u62e9\u5e8f\u5217\u91cc\u7684\u5bf9\u8c61\uff0c\u8c03\u7528\u65b9\u4e0d\u5fc5\u5173\u5fc3\u5e95\u5c42\u7ed3\u6784\u3002\u5b83\u901a\u5e38\u662f\u201c\u8f7b\u91cf\u7ea7\u201d\u7684\uff0c\u521b\u5efa\u4ee3\u4ef7\u5c0f\uff1b\u9650\u5236\u662f\u6709\u7684\u8fed\u4ee3\u5668\u53ea\u80fd\u5355\u5411\u79fb\u52a8\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Collection.iterator()<\/code> \u8fd4\u56de <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Iterator<\/code>\uff0c\u548c\u8f83\u65e9\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Enumeration<\/code> \u7c7b\u4f3c\uff1a\u4ece\u5934\u5230\u5c3e\u904d\u5386\uff0c\u5e76\u53ef\u4ee5\u5b89\u5168\u5730\u4ece\u5e95\u5c42\u96c6\u5408\u91cc\u5220\u9664\u5143\u7d20\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u65b9\u6cd5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">iterator()<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5bb9\u5668\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff1b\u7b2c\u4e00\u6b21 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">next()<\/code> \u5f97\u5230\u7b2c\u4e00\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">next()<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">hasNext()<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u662f\u5426\u8fd8\u6709\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">remove()<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5220\u9664<strong style=\"color: #4870ac;\">\u4e0a\u4e00\u6b21<\/strong><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\"> next()<\/code> \u8fd4\u56de\u7684\u90a3\u4e2a\u5143\u7d20\uff1b\u6bcf\u6b21 next \u540e\u6700\u591a\u8c03\u7528\u4e00\u6b21<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u4e00\u6bb5\u5b8c\u6574\u793a\u4f8b<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import java.util.ArrayList;\nimport java.util.Collection;\nimport java.util.Iterator;\n\npublic class IteratorDemo {\n    public static void main(String[] args) {\n        Collection collection = new ArrayList();\n        collection.add(\"s1\");\n        collection.add(\"s2\");\n        collection.add(\"s3\");\n\n        Iterator iterator = collection.iterator();\n        while (iterator.hasNext()) {\n            Object element = iterator.next();\n            System.out.println(\"iterator = \" + element);\n        }\n        if (collection.isEmpty())\n            System.out.println(\"collection is Empty!\");\n        else\n            System.out.println(\"collection is not Empty! size=\" + collection.size());\n\n        Iterator iterator2 = collection.iterator();\n        while (iterator2.hasNext()) {\n            Object element = iterator2.next();\n            System.out.println(\"remove: \" + element);\n            iterator2.remove();\n        }\n\n        Iterator iterator3 = collection.iterator();\n        if (!iterator3.hasNext()) {\n            System.out.println(\"\u6ca1\u6709\u66f4\u591a\u5143\u7d20\");\n        }\n\n        if (collection.isEmpty())\n            System.out.println(\"collection is Empty!\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd0\u884c\u7ed3\u679c\uff08\u539f\u6587\u5728\u7a7a\u96c6\u5408\u4e0a\u6253\u5370\u201c\u8fd8\u6709\u5143\u7d20\u201d\uff0c\u4e0e <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">!hasNext()<\/code> \u77db\u76fe\uff0c\u5df2\u6539\u6b63\uff09\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">iterator = s1\niterator = s2\niterator = s3\ncollection is not Empty! size=3\nremove: s1\nremove: s2\nremove: s3\n\u6ca1\u6709\u66f4\u591a\u5143\u7d20\ncollection is Empty!<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u4f7f\u7528\u8981\u70b9<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u5148 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">iterator()<\/code> \u518d\u5faa\u73af <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">hasNext()<\/code> \/ <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">next()<\/code><\/section>\n<\/li>\n<li>\n<section>\u5220\u9664\u5fc5\u987b\u8d70\u8fed\u4ee3\u5668\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">remove()<\/code>\uff0c\u4e0d\u8981\u5728\u904d\u5386\u65f6\u76f4\u63a5\u6539\u96c6\u5408<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">remove()<\/code> \u5220\u9664\u7684\u662f\u6700\u8fd1\u4e00\u6b21 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">next()<\/code> \u7ed9\u51fa\u7684\u5143\u7d20<\/section>\n<\/li>\n<li>\n<section>List \u8fd8\u6709\u66f4\u5f3a\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ListIterator<\/code>\uff08\u53ef\u53cc\u5411\u3001\u53ef\u6309\u4e0b\u6807\uff09\uff0c\u5728 List \u4e13\u9898\u91cc\u518d\u5c55\u5f00<\/section>\n<\/li>\n<\/ul>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u4e0d\u60f3\u7ed1\u6b7b ArrayList \u8fd8\u662f Set\uff0c\u5c31\u7528 Iterator \u904d\u5386\uff1ahasNext \/ next \u53d6\u503c\uff0c\u5220\u9664\u53ea\u80fd iterator.remove()\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4efb\u4f55\u5bb9\u5668\u90fd\u5fc5\u987b\u80fd\u628a\u4e1c\u897f\u653e\u8fdb\u53bb\u3001\u518d\u53d6\u51fa\u6765\u3002\u5bf9 ArrayList\uff0cadd() \u8d1f\u8d23\u63d2\u5165\uff0cget() \u662f\u53d6\u51fa\u65b9\u5f0f\u4e4b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[177],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/650"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}