2011. 6. 29. 10:49

자바(Java), LinkedList 클래스와 Iterator 클래스

import java.util.LinkedList ;
import java.util.Iterator ;
import java.util.StringTokenizer ;

public class IteratorEnumeration {
    public static void main(String[] args)     {
        LinkedList<String> queueStr = new LinkedList<String>() ;
        LinkedList<Integer> queueInt = new LinkedList<Integer>() ;

        String str = "트랜스포머3, 해리포터와죽음의성물2부, 풍산개, 써니, 수퍼에이트" ;
        StringTokenizer stknz = new StringTokenizer(str, ", ") ;

        Integer[] intArray = {1, 2, 3, 4, 5} ;

        while (stknz.hasMoreTokens()) {
            queueStr.offer(stknz.nextToken()) ;            
        }        

        for (int i = 0 ; i < intArray.length ; i++) {
            queueInt.add(intArray[i]) ;
        }

        System.out.println("LinkedList 출력") ;
        while (queueStr.peek() != null) {
            System.out.print(queueStr.poll() + "\t") ;                                
        }
        
        System.out.println() ;
    
        //Iterator를 이용한  출력
        Iterator<String> iterStr = queueStr.iterator() ;

        System.out.println("Iterator를 이용한 출력") ;
        while (iterStr.hasNext()) {
            System.out.print(iterStr.next() + "\t") ;            
        }
        
        System.out.println() ;

        Iterator<Integer> iterInt = queueInt.iterator() ;

        while (iterInt.hasNext()) {
            System.out.print(iterInt.next() + "\t") ;
        }

        System.out.println() ;
    }
}

Comment 0