标签:
例1
public static void main(String[]sdf){ List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); Iterator<String> it = list.iterator(); if(it.hasNext()){ it.remove(); } System.out.println(list.toString()); }
Console:
Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
例2
public static void main(String[]sdf){ List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); Iterator<String> it = list.iterator(); if(it.hasNext()){ it.next(); it.remove(); } System.out.println(list.toString()); }
Console:
[2, 3, 4]
例3
public static void main(String[]sdf){ List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); Iterator<String> it = list.iterator(); if(it.hasNext()){ it.next(); it.next(); it.remove(); } System.out.println(list.toString()); }
Console:
[1, 3, 4]
例4
public static void main(String[]sdf){ List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); Iterator<String> it = list.iterator(); if(it.hasNext()){ it.next(); it.remove(); it.remove(); } System.out.println(list.toString()); }
Console:
Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
附:
void remove()
UnsupportedOperationException
- 如果迭代器不支持 remove 操作。IllegalStateException
- 如果尚未调用 next 方法,或者在上一次调用 next 方法之后已经调用了remove 方法。标签:
原文地址:http://www.cnblogs.com/zno2/p/4495204.html