Collections Framework 3 : ArrayList

이의준·2024년 6월 7일

Java

목록 보기
61/87

ArrayList

  • 기존의 Vector를 개선한 것으로 구현원리와 기능적으로 동일
  • Vector와 달리 ArrayList는 동기화처리가 되어있지 않음
  • List 인터페이스를 구현하므로, 저장순서가 유지되고 증복을 허용
  • 데이터의 저장공간으로 배열을 사용함 (배열기반)
  • 객체만 저장 가능

ArrayList의 메서드

생성자

  • ArrayList() // 기본 생성자
  • ArrayList(Collection c) // 매개변수로 컬렉션을 받는 ArrayList 가능
  • ArrayList(int initialCapacity) // 배열의 길이

추가 기능 메서드

  • boolean add(Object o) // 성공하면 true 실패하면 false
  • void add(int index, Object element) // 저장 위치
  • boolean addAll(Collection c) // 컬렉션이 가지고 있는 요소 저장
  • boolean addAll(int index, Collection c)

삭제 기능 메서드

  • boolean remove(Object o)
  • Object remove(int index)
  • boolean remove(Collection c)
  • void clear() // 모든 객체 삭제

검색 기능 메서드

  • int indexOf(Object o) // 객체가 몇 번째 저장되어 있는지 (못찾으면 -1)
  • int lastIndexOf(Object o) // 반대 순서로 탐색
  • boolean contains(Object o) // 객체를 포함하고 있는지
  • Object get(int index) // 객체 읽기
  • Object set(int index, Object element) // 객체 변경
  • List subList(int fromIndex, int toIndex) // 일부만 선택해서 새로운 리스트 생성
  • Object[] toArray() // ArrayList의 객체배열을 반환
  • boolean isEmpty() // 비어있는지
  • void trimToSize() // 빈공간 제거
  • int size() // 저장된 객체의 개수


add와 subList 메서드 예시

    public static void main(String[] args) {
        ArrayList list1 = new ArrayList(10);
        list1.add(5); // ArrayList에는 객체만 들어감 그러나 autoboxing에 의해 이렇게 써도 참조형으로 변환됨
        list1.add(4);
        list1.add(2);
        list1.add(0);
        list1.add(1);
        list1.add(3);

        ArrayList list2 = new ArrayList(list1.subList(1, 4));
        System.out.println("list 1 : " + list1);
        System.out.println("List 2 : " + list2);
    }
  • 출력
    list 1 : [5, 4, 2, 0, 1, 3]
    List 2 : [4, 2, 0]


Collections.sort 메서드 예시

        Collections.sort(list1);
        Collections.sort(list2);
        System.out.println("list1 : " + list1);
        System.out.println("list2 : " + list2);
  • 출력
    list1 : [0, 1, 2, 3, 4, 5]
    list2 : [0, 2, 4]


containsAll 메서드 예시

System.out.println("list1.containsAll(list2) : " + list1.containsAll(list2));
// list1이 list2를 포함하고 있느냐
  • 출력
    list1.containsAll(list2) : true


add 메서드 예시

        list2.add("B");
        list2.add("C");
        list2.add(3, "A");
        System.out.println("list 1 : " + list1);
        System.out.println("List 2 : " + list2);
  • 출력
    list 1 : [0, 1, 2, 3, 4, 5]
    List 2 : [0, 2, 4, A, B, C]


set 메서드 예시

        list2.set(3, "AA");
        System.out.println("list 1 : " + list1);
        System.out.println("List 2 : " + list2);
  • 출력
    list 1 : [0, 1, 2, 3, 4, 5]
    List 2 : [0, 2, 4, AA, B, C]


indexOf 메서드 예시

        list1.add(0, "1");
        System.out.println("list 1 : " + list1);

        System.out.println("index=" + list1.indexOf("1")); // 문자열 1의 인덱스 찾기
        System.out.println("index=" + list1.indexOf(1)); // 정수 1의 인덱스 찾기
  • 출력
    list 1 : [1, 0, 1, 2, 3, 4, 5]
    index=0
    index=2


remove 메서드 예시

        System.out.println("list 1 : " + list1);
        list1.remove(new Integer(1));
        System.out.println("list 1 : " + list1);
        list1.remove(5);
        System.out.println("list 1 : " + list1);
  • 출력
    list 1 : [1, 0, 1, 2, 3, 4, 5]
    list 1 : [1, 0, 2, 3, 4, 5]
    list 1 : [1, 0, 2, 3, 4]


retainAll 메서드 예시

        System.out.println("list 1 : " + list1);
        System.out.println("list 2 : " + list2);
        System.out.println("list1.retainAll(list2) : " + list1.retainAll(list2));
        System.out.println("list 1 : " + list1);
        System.out.println("list 2 : " + list2);

-출력
list 1 : [1, 0, 2, 3, 4]
list 2 : [0, 2, 4, AA, B, C]
list1.retainAll(list2) : true
list 1 : [0, 2, 4]
list 2 : [0, 2, 4, AA, B, C]
(리스트1에서 리스트2와의 교집합만 뺴고 다 제거)



0개의 댓글