11-7~11 ArrayList
- ArrayList는 기존의 Vector를 개선한 것으로 구현 원리와 기능적으로 동일
(Array와 달리 Vector는 자체적으로 동기화 처리되어 있음) - List 인터페이스를 구현하므로, 저장순서가 유지되고 중복을 허용한다.
- 데이터의 저장공간으로 배열을 사용한다.(배열 기반)
ArrayList의 메서드 실습
public static void main(String[] args) {
// 기본 길이(용량, capacity)가 10인 ArrayList를 생성
ArrayList list1 = new ArrayList(10);
// ArrayList에는 객체만 저장가능
// autoboxing에 의해 기본형이 참조형으로 자동 변환
list1.add(new Integer(5)); // list1.add(5); OK
list1.add(new Integer(4));
list1.add(new Integer(2));
list1.add(new Integer(0));
list1.add(new Integer(1));
list1.add(new Integer(3));
List sub = list1.subList(1, 4); // sub는 읽기만 가능 [4, 2, 0]
ArrayList list2 = new ArrayList(sub); // sub 값을 갖는 ArrayList 생성
Collections.sort(list1); // list1 = [0, 1, 2, 3, 4, 5]
Collections.sort(list2); // list2 = [0, 2, 4]
System.out.println(list1.containsAll(list2)); // true
list2.add(3, "A"); // list2 = [0, 2, 4, A]
list2.set(3, "AA"); // list2 = [0, 2, 4, AA]
list1.add(0, "1"); // list1 = [1, 0, 1, 2, 3, 4, 5]
// indexOf()는 지정된 객체의 위치)를 알려준다. indexOf("1") OK
System.out.println(list1.indexOf(new String("1")));
list1.remove(new Integer(1)); // list1 = [1, 0, 2, 3, 4, 5]
list1.remove(1); // 인덱스가 1인 객체를 삭제 list1 = [1, 2, 3, 4, 5]
// list1에서 list2와 겹치는 부분만 남기고 나머지 삭제
System.out.println(list1.retainAll(list2)); // true
// list1 = [2, 4]
}
Author And Source
이 문제에 관하여(11-7~11 ArrayList), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@oyeon/11-711-ArrayList저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)