list = new ArrayList<>(); \r\n list.add(\"1\"); \r\n list.add(\"2\"); \r\n list.add(\"3\"); \r\n Iterator iterator = list.iterator(); \r\n while (iterator.hasNext()) { \r\n String str = (String) iterator.next(); \r\n if (str.equals(\"2\")) { \r\n iterator.remove(); \r\n } \r\n } \r\n while (iterator.hasNext()) { \r\n System.out.println(iterator.next()); \r\n } \r\n System.out.println(\"4\"); \r\n } \r\n} \r\n<\/string><\/pre>\n\u4ed6\u5199\u51fa\u6765\u7684\u7b54\u6848\u662f\uff1a<\/p>\n
\r\n1 \r\n3 \r\n4 \r\n<\/pre>\n\u5947\u602a\u7684\u662f\uff0c\u4f60\u628a\u8fd9\u9053\u9898\u76ee\u53d1\u7ed9\u4f60\u8eab\u8fb9\u4eba\uff0c\u8ba9\u4ed6\u4eec\u56de\u7b54\u8fd9\u9053\u9762\u8bd5\u9898\u8f93\u51fa\u7ed3\u679c\u662f\u4ec0\u4e48\uff0c\u8bf4\u8fd9\u4e2a\u7ed3\u679c\u7684\u4eba\u975e\u5e38\u591a\u3002\u4e0d\u884c\u4f60\u8bd5\u8bd5\u3002<\/p>\n
\u7b54\u6848\u660e\u663e\u4e0d\u5bf9\uff0c\u56e0\u4e3a\u5728\u7b2c\u4e00\u4e2awhile\u91cc\u7684 iterator.hasNext()==false\u540e\u624d\u4f1a\u5230\u7b2c\u4e8c\u4e2awhile\u91cc\u6765\uff0c\u540c\u4e00\u4e2aIterator\u5bf9\u8c61\uff0c\u524d\u9762\u8c03\u4e00\u6b21iterator.hasNext()==false\uff0c\u518d\u5224\u65ad\u4e00\u6b21\u7ed3\u679c\u4e0d\u8fd8\u662f\u4e00\u6837\u5417?\uff0c<\/p>\n
\u6240\u4ee5\u7b2c\u4e8c\u4e2awhile\u5224\u65ad\u4e3afalse\uff0c\u4e5f\u5c31\u4e0d\u4f1a\u518d\u53bb\u904d\u5386iterator\u4e86\uff0c\u7531\u6b64\u53ef\u77e5\u672c\u4f53\u7b54\u6848\u662f\uff1a4\u3002<\/p>\n
\u4e0b\u9762\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u662f\u5177\u4f53\u5e95\u5c42\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002<\/p>\n
\u8fd9\u91cc\u7684Iterator\u662f\u4ec0\u4e48?<\/p>\n
\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u6a21\u5f0f\u3001\u8be6\u7ec6\u53ef\u89c1\u5176\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u5f97\u5e8f\u5217\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u7684\u904d\u5386\u884c\u4e3a\u4e0e\u88ab\u904d\u5386\u7684\u5bf9\u8c61\u5206\u79bb\uff0c\u5373\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u8be5\u5e8f\u5217\u7684\u5e95\u5c42\u7ed3\u6784\u662f\u4ec0\u4e48\u6837\u5b50\u7684\u3002\u53ea\u8981\u62ff\u5230\u8fd9\u4e2a\u5bf9\u8c61,\u4f7f\u7528\u8fed\u4ee3\u5668\u5c31\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5bf9\u8c61\u7684\u5185\u90e8
\nIterable \u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684\u96c6\u5408\u5bf9\u8c61\u652f\u6301\u8fed\u4ee3\uff0c\u662f\u53ef\u4ee5\u8fed\u4ee3\u7684\u3002\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u53ef\u4ee5\u914d\u5408foreach\u4f7f\u7528~
\nIterator \u8fed\u4ee3\u5668\uff0c\u63d0\u4f9b\u8fed\u4ee3\u673a\u5236\u7684\u5bf9\u8c61\uff0c\u5177\u4f53\u5982\u4f55\u8fed\u4ee3\u662f\u8fd9\u4e2aIterator\u63a5\u53e3\u89c4\u8303\u7684\u3002
\nIterator\u8bf4\u660e<\/p>\n
\r\npublic interface Iterator { \r\n \/\/\u6bcf\u6b21next\u4e4b\u524d\uff0c\u5148\u8c03\u7528\u6b64\u65b9\u6cd5\u63a2\u6d4b\u662f\u5426\u8fed\u4ee3\u5230\u7ec8\u70b9 \r\n boolean hasNext(); \r\n \/\/\u8fd4\u56de\u5f53\u524d\u8fed\u4ee3\u5143\u7d20 \uff0c\u540c\u65f6\uff0c\u8fed\u4ee3\u6e38\u6807\u540e\u79fb \r\n E next(); \r\n \/*\u5220\u9664\u6700\u8fd1\u4e00\u6b21\u5df2\u8fd1\u8fed\u4ee3\u51fa\u51fa\u53bb\u7684\u90a3\u4e2a\u5143\u7d20\u3002 \r\n \u53ea\u6709\u5f53next\u6267\u884c\u5b8c\u540e\uff0c\u624d\u80fd\u8c03\u7528remove\u51fd\u6570\u3002 \r\n \u6bd4\u5982\u4f60\u8981\u5220\u9664\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528 remove() \u800c\u8981\u5148next\u4e00\u4e0b( ); \r\n \u5728\u6ca1\u6709\u5148\u8c03\u7528next \u5c31\u8c03\u7528remove\u65b9\u6cd5\u662f\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u3002 \r\n \u8fd9\u4e2a\u548cMySQL\u4e2d\u7684ResultSet\u5f88\u7c7b\u4f3c \r\n *\/ \r\n default void remove() { \r\n throw new UnsupportedOperationException(\"remove\"); \r\n } \r\n default void forEachRemaining(Consumer super E> action) { \r\n Objects.requireNonNull(action); \r\n while (hasNext()) \r\n action.accept(next()); \r\n } \r\n} \r\n<\/e><\/pre>\n\u8fd9\u91cc\u7684\u5b9e\u73b0\u7c7b\u662fArrayList\u7684\u5185\u90e8\u7c7bItr\u3002<\/p>\n
\r\nprivate class Itr implements Iterator { \r\n int cursor; \/\/ index of next element to return \r\n int lastRet = -1; \/\/ index of last element returned; -1 if no such \r\n \/\/modCountshi ArrayList\u4e2d\u7684\u5c5e\u6027\uff0c\u5f53\u6dfb\u52a0\u6216\u5220\u9664\u7684\u65f6\u5019moCount\u503c\u4f1a\u589e\u52a0\u6216\u8005\u51cf\u5c11 \r\n \/\/\u8fd9\u91cc\u4e3b\u8981\u662f\u7ed9fail-fast\u4f7f\u7528\uff0c\u907f\u514d\u4e00\u904d\u5728\u904d\u5386\uff0c\u4e00\u904d\u6b63\u5728\u4fee\u6539\u5bfc\u81f4\u6570\u636e\u51fa\u9519 \r\n \/\/\u6b64\u5217\u8868\u5728\u7ed3\u6784\u4e0a\u88ab\u4fee\u6539\u7684\u6b21\u6570\u3002\u7ed3\u6784\u4fee\u6539\u662f\u6307\u6539\u53d8\u7ed3\u6784\u5c3a\u5bf8\u7684\u4fee\u6539\u5217\u8868\uff0c \r\n \/\/\u6216\u8005\u4ee5\u8fd9\u6837\u7684\u65b9\u5f0f\u5bf9\u5176\u8fdb\u884c\u6270\u52a8,\u8fdb\u6b65\u53ef\u80fd\u4f1a\u4ea7\u751f\u9519\u8bef\u7684\u7ed3\u679c\u3002 \r\n int expectedModCount = modCount; \r\n \r\n public boolean hasNext() { \r\n \/\/cursor\u521d\u59cb\u503c\u4e3a0\uff0c\u6ca1\u6389\u4e00\u6b21next\u65b9\u6cd5\u5c31+1 \r\n \/\/size\u662fArrayList\u7684\u5927\u5c0f \r\n return cursor != size; \r\n } \r\n \r\n @SuppressWarnings(\"unchecked\") \r\n public E next() { \r\n checkForComodification(); \r\n int i = cursor; \r\n if (i >= size) \r\n throw new NoSuchElementException(); \r\n \/\/\u628aArrayList\u4e2d\u7684\u6570\u7ec4\u8d4b\u7ed9elementData \r\n Object[] elementData = ArrayList.this.elementData; \r\n if (i >= elementData.length) \r\n throw new ConcurrentModificationException(); \r\n \/\/\u6bcf\u8c03\u7528\u4e00\u6b21next\u65b9\u6cd5\uff0c\u6e38\u6807\u5c31\u52a01 \r\n \/\/cursor=lastRet+1 \r\n cursor = i + 1; \r\n \/\/\u8fd4\u56deArrayList\u4e2d\u7684\u5143\u7d20 \r\n return (E) elementData[lastRet = i]; \r\n } \r\n \r\n public void remove() { \r\n if (lastRet < 0) \r\n throw new IllegalStateException(); \r\n checkForComodification(); \r\n \r\n try { \r\n \/\/\u8c03\u7528ArrayList\u4e2dremove\u65b9\u6cd5\uff0c\u6ea2\u51fa\u8be5\u5143\u7d20 \r\n ArrayList.this.remove(lastRet); \r\n \/\/cursor=lastRet+1\uff0c \r\n \/\/\u6240\u4ee5\u6b64\u65f6\u76f8\u5f53\u4e8ecursor=cursor-1 \r\n cursor = lastRet; \r\n lastRet = -1; \r\n expectedModCount = modCount; \r\n } catch (IndexOutOfBoundsException ex) { \r\n throw new ConcurrentModificationException(); \r\n } \r\n } \r\n final void checkForComodification() { \r\n if (modCount != expectedModCount) \r\n throw new ConcurrentModificationException(); \r\n } \r\n} \r\n<\/e><\/pre>\n\u518d\u56de\u5230\u4e0a\u9762\u9898\u76ee\u4e2d\uff1a<\/strong><\/div>\n\u7b2c\u4e00\u4e2aiterator.hasNext()<\/p>\n
\u7b2c1\u6b21\u5faa\u73af<\/strong><\/span><\/div>\nhasNext\u65b9\u6cd5\u4e2d\uff1acursor==0\uff0c size==3\uff0c\u6240\u4ee5cursor != size\u8fd4\u56detrue\u3002
\nnext\u65b9\u6cd5\u4e2d\uff1acursor=0+1\u3002\u8fd4\u56de\"1\"\u3002<\/p>\n
\u7b2c2\u6b21\u5faa\u73af<\/strong><\/span><\/div>\nhasNext\u65b9\u6cd5\u4e2d\uff1acursor==1\uff0c size==3\uff0c\u6240\u4ee5cursor != size\u8fd4\u56detrue\u3002
\nnext\u65b9\u6cd5\u4e2d\uff1acursor=1+1\u3002\u8fd4\u56de\"2\"\u3002
\nremove\u65b9\u6cd5\u4e2d\uff1acursor==cursor-1==2-1=1\uff0c\u628aArrayList\u4e2d\u7684\"2\"\u7ed9\u5220\u9664\u4e86\uff0c\u6240\u4ee5size==2\u3002<\/p>\n
\u7b2c3\u6b21\u5faa\u73af<\/strong><\/span><\/div>\nhasNext\u65b9\u6cd5\u4e2d\uff1acursor==1\uff0c size==2\uff0c\u90a3\u4e48cursor != size\u8fd4\u56detrue\u3002
\nnext\u65b9\u6cd5\u4e2d\uff1acursor=1+1==2;\u8fd4\u56de\"3\"\u3002<\/p>\n
\u7b2c4\u6b21\u5faa\u73af<\/strong><\/span><\/div>\nhasNext\u65b9\u6cd5\u4e2d\uff1acursor==2\uff0c size==2\uff0c\u90a3\u4e48cursor != size\u8fd4\u56defalse\u3002
\n\u7b2c\u4e8c\u4e2aiterator.hasNext()<\/p>\n
hasNext\u65b9\u6cd5\u4e2d\uff1acursor==2\uff0c size==2\uff0c\u6240\u4ee5cursor != size\u8fd4\u56defalse\u3002<\/p>\n
\u6240\u4ee5\uff0c\u6700\u540e\u53ea\u8f93\u51fa\"4\"\uff0c\u5373\u7b54\u6848\u4e3a4.<\/p>\n
Iterator\u4e0e\u6cdb\u578b\u642d\u914d<\/strong><\/div>\nIterator\u5bf9\u96c6\u5408\u7c7b\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u90fd\u53ef\u4ee5\u8fd4\u56de\u8fd9\u6837\u4e00\u4e2aIterator\u5bf9\u8c61\u3002\u53ef\u4ee5\u9002\u7528\u4e8e\u4efb\u4f55\u4e00\u4e2a\u7c7b\u3002
\n\u56e0\u4e3a\u96c6\u5408\u7c7b(List\u548cSet\u7b49)\u53ef\u4ee5\u88c5\u5165\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u4e0d\u786e\u5b9a\u7684,\u4ece\u96c6\u5408\u4e2d\u53d6\u51fa\u65f6\u90fd\u662fObject\u7c7b\u578b,\u7528\u65f6\u90fd\u9700\u8981\u8fdb\u884c\u5f3a\u5236\u8f6c\u5316,\u8fd9\u6837\u4f1a\u5f88\u9ebb\u70e6,\u7528\u4e0a\u6cdb\u578b,\u5c31\u662f\u63d0\u524d\u544a\u8bc9\u96c6\u5408\u786e\u5b9a\u8981\u88c5\u5165\u96c6\u5408\u7684\u7c7b\u578b,\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u4e0d\u7528\u663e\u793a\u7c7b\u578b\u8f6c\u6362.\u975e\u5e38\u65b9\u4fbf.<\/p>\n
foreach\u548cIterator\u7684\u5173\u7cfb<\/strong><\/div>\nfor each\u4ee5\u7528\u6765\u5904\u7406\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u800c\u4e0d\u7528\u8003\u8651\u96c6\u5408\u5b9a\u4e0b\u6807\u3002\u5c31\u662f\u4e3a\u4e86\u8ba9\u7528Iterator\u7b80\u5355\u3002\u4f46\u662f\u5220\u9664\u7684\u65f6\u5019\uff0c\u533a\u522b\u5c31\u662f\u5728remove\uff0c\u5faa\u73af\u4e2d\u8c03\u7528\u96c6\u5408remove\u4f1a\u5bfc\u81f4\u539f\u96c6\u5408\u53d8\u5316\u5bfc\u81f4\u9519\u8bef\uff0c\u800c\u5e94\u8be5\u7528\u8fed\u4ee3\u5668\u7684remove\u65b9\u6cd5\u3002<\/p>\n
\u4f7f\u7528for\u5faa\u73af\u8fd8\u662f\u8fed\u4ee3\u5668Iterator\u5bf9\u6bd4<\/strong><\/div>\n\u91c7\u7528ArrayList\u5bf9\u968f\u673a\u8bbf\u95ee\u6bd4\u8f83\u5feb\uff0c\u800cfor\u5faa\u73af\u4e2d\u7684get()\u65b9\u6cd5\uff0c\u91c7\u7528\u7684\u5373\u662f\u968f\u673a\u8bbf\u95ee\u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u5728ArrayList\u91cc\uff0cfor\u5faa\u73af\u8f83\u5feb
\n\u91c7\u7528LinkedList\u5219\u662f\u987a\u5e8f\u8bbf\u95ee\u6bd4\u8f83\u5feb\uff0citerator\u4e2d\u7684next()\u65b9\u6cd5\uff0c\u91c7\u7528\u7684\u5373\u662f\u987a\u5e8f\u8bbf\u95ee\u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\u5728LinkedList\u91cc\uff0c\u4f7f\u7528iterator\u8f83\u5feb
\n\u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u5206\u6790,for\u5faa\u73af\u9002\u5408\u8bbf\u95ee\u987a\u5e8f\u7ed3\u6784,\u53ef\u4ee5\u6839\u636e\u4e0b\u6807\u5feb\u901f\u83b7\u53d6\u6307\u5b9a\u5143\u7d20.\u800cIterator \u9002\u5408\u8bbf\u95ee\u94fe\u5f0f\u7ed3\u6784,\u56e0\u4e3a\u8fed\u4ee3\u5668\u662f\u901a\u8fc7next()\u548cPre()\u6765\u5b9a\u4f4d\u7684.\u53ef\u4ee5\u8bbf\u95ee\u6ca1\u6709\u987a\u5e8f\u7684\u96c6\u5408.
\n\u800c\u4f7f\u7528 Iterator \u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u65b9\u5f0f\u53bb\u904d\u5386\u96c6\u5408\u4e2d\u5143\u7d20\uff0c\u800c\u4e0d\u7528\u8003\u8651\u96c6\u5408\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0(\u53ea\u8981\u5b83\u5b9e\u73b0\u4e86 java.lang.Iterable \u63a5\u53e3)\uff0c\u5982\u679c\u4f7f\u7528 Iterator \u6765\u904d\u5386\u96c6\u5408\u4e2d\u5143\u7d20\uff0c\u4e00\u65e6\u4e0d\u518d\u4f7f\u7528 List \u8f6c\u800c\u4f7f\u7528 Set \u6765\u7ec4\u7ec7\u6570\u636e\uff0c\u90a3\u904d\u5386\u5143\u7d20\u7684\u4ee3\u7801\u4e0d\u7528\u505a\u4efb\u4f55\u4fee\u6539\uff0c\u5982\u679c\u4f7f\u7528 for \u6765\u904d\u5386\uff0c\u90a3\u6240\u6709\u904d\u5386\u6b64\u96c6\u5408\u7684\u7b97\u6cd5\u90fd\u5f97\u505a\u76f8\u5e94\u8c03\u6574,\u56e0\u4e3aList\u6709\u5e8f,Set\u65e0\u5e8f,\u7ed3\u6784\u4e0d\u540c,\u4ed6\u4eec\u7684\u8bbf\u95ee\u7b97\u6cd5\u4e5f\u4e0d\u4e00\u6837.(\u8fd8\u662f\u8bf4\u660e\u4e86\u4e00\u70b9\u904d\u5386\u548c\u96c6\u5408\u672c\u8eab\u5206\u79bb\u4e86)\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/div>\n\u8fed\u4ee3\u51fa\u6765\u7684\u5143\u7d20\u90fd\u662f\u539f\u6765\u96c6\u5408\u5143\u7d20\u7684\u62f7\u8d1d\u3002
\nJava\u96c6\u5408\u4e2d\u4fdd\u5b58\u7684\u5143\u7d20\u5b9e\u8d28\u662f\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u800c\u975e\u5bf9\u8c61\u672c\u8eab\u3002
\n\u8fed\u4ee3\u51fa\u7684\u5bf9\u8c61\u4e5f\u662f\u5f15\u7528\u7684\u62f7\u8d1d\uff0c\u7ed3\u679c\u8fd8\u662f\u5f15\u7528\u3002\u90a3\u4e48\u5982\u679c\u96c6\u5408\u4e2d\u4fdd\u5b58\u7684\u5143\u7d20\u662f\u53ef\u53d8\u7c7b\u578b\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u51fa\u7684\u5143\u7d20\u4fee\u6539\u539f\u96c6\u5408\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6709\u4f4d\u5c0f\u670b\u53cb\u6700\u8fd1\u6b63\u5728\u4e3a\u5e74\u540e\u6362\u5de5\u4f5c\u505a\u51c6\u5907\uff0c\u4f46\u662f\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u89c9\u5f97\u5f88\u4e0d\u53ef\u601d\u8bae\u7684\u4e00\u9053\u7b14\u8bd5\u9898\u3002\u7136\u540e\u6211\u628a\u8fd9\u9053\u9898\u53d1\u5230\u6280\u672f\u7fa4\u91cc […]<\/p>\n","protected":false},"author":1898,"featured_media":210887,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[652],"class_list":["post-210882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-652"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=210882"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210882\/revisions"}],"predecessor-version":[{"id":210888,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210882\/revisions\/210888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/210887"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=210882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=210882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=210882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}