ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • List 계열의 Collection / List 인터페이스 / ArrayList / LinkedList / 2019-12-09
    카테고리 없음 2020. 1. 15. 11:16
    728x90
    728x90

    List 계열은 인덱스 기반으로 데이터를 저장한다 (0~...)

    => 데이터 중복 허용

    - null 값도 저장 가능 

     

    ArrayList list = new ArrayList();
    list.add(1);
    list.add("TWO");
    list.add(3.14);
    
    if (!list.isEmpty()) {
      System.out.println("list의 size : " + list.size());	// list의 크기
      System.out.println("list : " + list);
    }
    System.out.println("list 객체에 정수 1추가가 가능한가? : " + list.add(1));
    System.out.println(list);
    list.add("4");
    list.add(5L);
    System.out.println(list);

    <결과코드>

    list의 size : 3
    list : [1, TWO, 3.14]
    list 객체에 정수 1추가가 가능한가? : true
    [1, TWO, 3.14, 1]
    [1, TWO, 3.14, 1, 4, 5]

    Set 계열과 달리 인덱스 형태로 데이터가 저장됨

    => 배열과 같이 index를 사용하여 데이터를 가져올 수 있다

    System.out.println("list 객체의 0번 index요소 : " + list.get(0));
    System.out.println("list 객체의 2번 index요소 : " + list.get(2));
    
    for(int i = 0; i < list.size(); i++) {
    	System.out.print(list.get(i) + " ");
    }

    <결과코드>

    list 객체의 0번 index요소 : 1
    list 객체의 2번 index요소 : 3.14
    1 TWO 3.14 1 4 5 
    

     

    Iterator 도 사용 가능

    Iterator it = list.iterator();
    while(it.hasNext()) {		// it에 데이터가 있을 때까지
    	Object o = it.next();	// it의 데이터 하나씩 반환
    	System.out.print(o + " ");
    }
    list.add(2, 3);
    System.out.println(list);

    add(int index, E element) 를 사용하면

    index에 element가 삽입되고 index이후의 값들은 한 칸 씩 밀려남

     

    List subList = list.subList(2, 4);
    System.out.println("list객체의 서브리스트" + subList);
    System.out.println("list는 subList의 모든 요소를 포함하는가? : " + list.containsAll(subList));
    list.clear();
    System.out.println(list);

    <결과코드>

    list객체의 서브리스트[3.14, 1]
    list는 subList의 모든 요소를 포함하는가? : true
    []

     

    728x90
    728x90
Designed by Tistory.