자바 집합 프레임 워 크 listIterator 인터페이스

1751 단어 자바 학습
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;

public class ListDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List list=new ArrayList();
		//show(list);
		list.add("abc1");
		list.add("abc2");
		list.add("abc3");

		java.util.ListIterator it= list.listIterator();
		//         
		//                     
		//  :  List         
		
		while(it.hasNext()) {
			Object obj=it.next();
			
			if(obj.equals("abc2")) {
				it.set("abc9");
			}
			
		}
		while(it.hasPrevious()) {
			System.out.println(it.previous());
		}
		System.out.println("next:"+it.hasNext());;
		System.out.println("next:"+it.hasPrevious());;
		System.out.println("list:"+list);
		
/*		Iterator it=list.iterator();
		while(it.hasNext()) {
			Object obj=it.next();
			if(obj.equals("abc2")) {
				list.add("abc9");//java.util.NoSuchElementException
								//               
								//         ,          ,      
								//    Iterator      ListIterator              。
				
			}
			else
				System.out.println("next:"+obj);
			
			System.out.println( "next:"+it.next());
			*/
		}		
		
	

	private static void show(List list) {
		// TODO Auto-generated method stub
		list.add("adb1");
		list.add("adb2");
		list.add("adb3");
		list.add("adb4");
		
		Iterator it=list.iterator();
		while(it.hasNext()) {
			System.out.println("next:"+it.next());
			
		}
		//List             
		for (int i = 0; i < list.size(); i++) {
			System.out.println("get:"+list.get(i));
		}
		
	}

}

좋은 웹페이지 즐겨찾기