it.remove();it.remove();//error而是应该it.remove();it.next();it.remove();
import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class LearnCollection {public static void main(String[] args) {// TODO Auto-generated method stubArrayList<String> nameList = new ArrayList<String>();nameList.add("zhang.san");nameList.add("li.si");nameList.add("wang.wu");nameList.add("zhao.yi");System.out.println("initial content.");printCollection(nameList);printViaIterator(nameList.iterator());removeViaIterator(nameList.iterator(), "li.si");System.out.println("");System.out.println("after remove li.si");printCollection(nameList);printViaIterator(nameList.iterator());}/*** remove via iterator* @param it* @param value*/static void removeViaIterator(Iterator<?> it, String value){while(it.hasNext()){if(it.next().toString() == value){it.remove();System.out.println(String.format("remove %s success.", value));return;}}System.out.println(String.format("remove %s failed.", value));}/*** @param it*/static void printViaIterator(Iterator<?> it){System.out.println("Print via iterator:");while(it.hasNext())System.out.println(String.format("\titem:%s", it.next().toString()));}/*** @param collection*/static void printCollection(Collection<?> collection){System.out.println("collection content:");for(Object item: collection){System.out.println(String.format("\titem:%s", item.toString()));}}}
static void testToArray(){LinkedList<String> strList = new LinkedList<String>();strList.add("zhang.san");strList.add("li.si");strList.add("wang.wu");print("filled array is not large enough.");testToArrayHelper(strList, new String[2]);print("filled array is large enough.");testToArrayHelper(strList, new String[3]);}static void testToArrayHelper(LinkedList<String> strList, String[] filledArray){String[] returnedArray = strList.toArray(filledArray);printArray("filled array:", filledArray);print("");printArray("returned array:", returnedArray);print("");if(filledArray == returnedArray)print("filled array is equal returned array.");elseprint("filled array is not equal returned array.");}static <T> void printArray(String title, T[] array){print(title);for(T item: array){if(item != null)print("item:" + item.toString());elseprint("item is null");}}static void print(String info){System.out.println(info);}
filled array is not large enough.filled array:item is nullitem is nullreturned array:item:zhang.sanitem:li.siitem:wang.wufilled array is not equal returned array.filled array is large enough.filled array:item:zhang.sanitem:li.siitem:wang.wureturned array:item:zhang.sanitem:li.siitem:wang.wufilled array is equal returned array.
static void testListIteratorAdd(){LinkedList<String> strList = new LinkedList<String>();strList.add("1");strList.add("2");strList.add("3");print("init content:");printCollection(strList);ListIterator<String> it = strList.listIterator();it.next();it.add("1.1");it.add("1.2");print("after insert 2 item");printCollection(strList);}
init content:collection content:item:1item:2item:3after insert 2 itemcollection content:item:1item:1.1item:1.2item:2item:3
static void testListIteratorRemove(){LinkedList<String> strList = new LinkedList<String>();strList.add("1");strList.add("2");strList.add("3");print("init content:");printCollection(strList);ListIterator<String> it = strList.listIterator();it.next();it.remove();//okprint("after remove 1 item");printCollection(strList);it.remove();//errorprint("after remove 2 item");printCollection(strList);}
init content:collection content:item:1item:2item:3after remove 1 itemcollection content:item:2item:3Exception in thread "main" java.lang.IllegalStateExceptionat java.util.LinkedList$ListItr.remove(LinkedList.java:923)at me.ygc.javabasic.learnJava.LearnCollection.testListIteratorRemove(LearnCollection.java:33)at me.ygc.javabasic.learnJava.LearnCollection.main(LearnCollection.java:15)
Java系列,《Java核心技术 卷1》,chapter 13,集合
原文:http://www.cnblogs.com/strinkbug/p/5062609.html