package com.crf.java.c7;
import java.util.*;public class TestIterator
{
public static void main(String[] args)
{
//创建一个集合
Collection books = new HashSet();
books.add("11111");
books.add("22222");
books.add("33333");
//获取books集合对应的迭代器
Iterator it = books.iterator();
while(it.hasNext())
{
String book = (String)it.next();
System.out.println(book);
if (book.equals("22222"))
{
it.remove();
}
book= "44444";
}
System.out.println(books);
}
}
运行结果 :
22222
33333
11111
[33333, 11111]书上说Iterotor()并不是集合元素本身传给了迭代变量 而是把集合元素的值传给了迭代变量 既然如此 那么为什么最后不输出[22222 33333 11111]呢?求解????
import java.util.*;public class TestIterator
{
public static void main(String[] args)
{
//创建一个集合
Collection books = new HashSet();
books.add("11111");
books.add("22222");
books.add("33333");
//获取books集合对应的迭代器
Iterator it = books.iterator();
while(it.hasNext())
{
String book = (String)it.next();
System.out.println(book);
if (book.equals("22222"))
{
it.remove();
}
book= "44444";
}
System.out.println(books);
}
}
运行结果 :
22222
33333
11111
[33333, 11111]书上说Iterotor()并不是集合元素本身传给了迭代变量 而是把集合元素的值传给了迭代变量 既然如此 那么为什么最后不输出[22222 33333 11111]呢?求解????
解决方案 »
- GetStringUTFChars(env,str,NULL)返回值的操作问题
- 菜鸟看<T>,实在不得不问。高手进来看看,帮忙指点一下~~~
- 读txt文件的问题
- j2ee5+tomcat6.0+myeclipse6.0(eclipse3.3 j2ee版的)如何配制
- java语言中有让最小化窗口在任务栏闪烁的方法吗?
- String类中的equal方法和Object类中的equal方法有什么区别
- 问个比较弱的问题:关于输入流缓冲
- 怎么样获得INSERT\NUM\CAPS LOCK这三个键的状态?
- 急急急急,百度地图图标标记
- 大难不死,必有后福 先来这么多,呵呵
- SpinnerDateModel中的calendarField属性有什么用?
- java环境变量怎么配置
你可以自己去看看API中Iterator关于remove函数的介绍remove
void remove()从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的 collection,则迭代器的行为是不确定的。 抛出:
UnsupportedOperationException - 如果迭代器不支持 remove 操作。
IllegalStateException - 如果尚未调用 next 方法,或者在上一次调用 next 方法之后已经调用了 remove 方法。
Entry<K,V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K,V> current; // current entry HashIterator() {
expectedModCount = modCount;
Entry[] t = table;
int i = t.length;
Entry<K,V> n = null;
if (size != 0) { // advance to first entry
while (i > 0 && (n = t[--i]) == null)
;
}
next = n;
index = i;
} public boolean hasNext() {
return next != null;
} Entry<K,V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
Entry<K,V> n = e.next;
Entry[] t = table;
int i = index;
while (n == null && i > 0)
n = t[--i];
index = i;
next = n;
return current = e;
} public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
/*Use the Entry get method instead of direct access to the key ibm@117216 */
Object k = current.getKey(); /*ibm@117216*/
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
} }
另外。迭代器在进行迭代的时候不允许另外的迭代器或原集合本身进行了修改
看下面代码Collection books = new HashSet();
books.add("11111");
books.add("22222");
books.add("33333");
//获取books集合对应的迭代器
Iterator it = books.iterator();
while(it.hasNext())
{
String book = (String)it.next();
System.out.println(book);
if (book.equals("11111"))
{
//it.remove();
books.remove("11111");
} book= "44444";
}
System.out.println(books);就会报上面的ConcurrentModificationException();
if (book.equals("22222"))
{
it.remove();//这里指迭代器指向的集合当前元素,对该元素进行删除
}
最后怎么可能还会有”22222“
222222它的index实际是0
333333它的index实际是1
111111它的index实际是2
这个迭代器它刚开始的位置是0的前面。当第一次调next();时,它的指针才指向index==0
于是这时你把222222给删除了。所以最后打印出333333和111111
当然没有22222这个元素啦~