2011. 6. 28. 15:45

자바(Java) Collections 인터페이스와 ArrayList 클래스

import java.util.Collections ;
import java.util.ArrayList ;
import java.util.Scanner ;

public class ArrayListTest2 {
    public static void main(String[] args)     {
        ArrayList<String> arraylist = new ArrayList<String>() ;
        Scanner scan = new Scanner(System.in) ;

        for (int i = 0 ; i < 6 ; i++) {
            System.out.print("영화 개봉작 : ") ;
            arraylist.add(scan.nextLine()) ;
        }

        System.out.println("\nArrayList의 size : " + arraylist.size()) ;
        System.out.println("ArrayList의 isEmpty : " + arraylist.isEmpty()) ;
        System.out.println("ArrayList의 contains : " + arraylist.contains("트랜스포머3")) ;
        System.out.println("\n" + arraylist + "\n") ;

        Collections.sort(arraylist) ;
        int index = Collections.binarySearch(arraylist, "트랜스포머3") ;
        System.out.println("찾는 위치 : " + index) ;
        System.out.println("\n" + arraylist + "\n") ;

        System.out.println("get(0) : " + arraylist.get(0)) ;        

        arraylist.set(0, "풍산개") ;
        System.out.println("get(0) : " + arraylist.get(0)) ;
        System.out.println("\n" + arraylist + "\n") ;

        arraylist.remove("풍산개") ;
        System.out.println("\n" + arraylist + "\n") ;

        arraylist.clear() ;
        System.out.println("\n" + arraylist + "\n") ;
    }
}