Java ArrayList
by 볼빵빵오춘기더보기


ArrayList는 List인터페이스 중 하나이다. (순서o,중복o)
ArrayList
- ArrayList는 기존의 Vector를 개선한 것으로 구현원리와 기능적으로 동일하나 ArrayList와 달리 Vector는 자체적으로 동기화처리가 되어있다.
- List인터페이스를 구현하므로, 저장순서가 유지되고 중복을 허용한다.
- 데이터의 저장공간으로 배열을 사용한다.(배열기반)
※ 이름에 List가 붙은것(ArrayList, LinkedList)들은 List인터페이스를 구현한 것

ArrayList의 메서드
ArrayList 생성자
- ArrayList()
- ArrayList(Collection c)
- ArrayList(int initialcapacity)
ArrayList 추가 기능 메서드
- boolean add(Object o)
- void add(int index, Object element)
- boolean addAll(Collection c)
- boolean addAll(int index, Collection c)
ArrayList 삭제 기능 메서드
- boolean remove(Object o)
- Object remove(int index)
- boolean removeAll(Collection c)
- void clear()
ArrayList 검색 기능 메서드
- int indexOf(Object o)
- int lastIndexOf(Object o)
- boolean contains(Object o)
- Object get(int index)
- Object set(int index, Object element)
ArrayList 그 외 메서드
- List subList(inf fromIndex, int toIndex)
- Object[] toArray()
- Object[] toArray(Object[] a)
- boolean isEmpty()
- void trimToSize()
- int size()
예제 코드
import java.util.ArrayList; import java.util.Collections; public class VarEx3 { public static void main(String[] args) { // 기본 길이(용량, capacity)가 10인 ArrayList 생성 ArrayList list1 = new ArrayList(10); // list1.add(new Integer(5)) // => 이렇게 적어야하지만 컴파일러 autoboxing 해줌. list1.add(new Integer(5)); 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)); ArrayList list2 = new ArrayList(list1.subList(1,4)); System.out.println("1- "); print(list1, list2); // Collection은 인터페이스, Collections는유틸 클래스 Collections.sort(list1); // list1과 list2를 정렬한다. Collections.sort(list2); // Collections.sort(List l) System.out.println("2- "); print(list1, list2); System.out.println("3- "); System.out.println("list1.containsAll(list2):" + list1.containsAll(list2)); // 추가 list2.add("B"); list2.add("C"); list2.add(3, "A"); // 끼워넣는것은 부담이 가는작업 System.out.println("4- "); print(list1, list2); // 변경 list2.set(3, "AA"); System.out.println("5- "); print(list1, list2); // list1에서 list2와 겹치는 부분만 남기고 나머지는 삭제한다. System.out.println("6- list1.retainAll(list2):" + list1.retainAll(list2)); System.out.println("7- "); print(list1, list2); // list2에서 list1에 포함된 객체들을 삭제한다. for(int i= list2.size()-1; i >= 0; i--) { if(list1.contains(list2.get(i))) list2.remove(i); } System.out.println("1- "); print(list1, list2); } // main의 끝 static void print(ArrayList list1, ArrayList list2) { System.out.println("list1:"+list1); System.out.println("list2:"+list2); System.out.println(); } }
1-
list1:[5, 4, 2, 0, 1, 3]
list2:[4, 2, 0]
2-
list1:[0, 1, 2, 3, 4, 5]
list2:[0, 2, 4]
3-
list1.containsAll(list2):true
4-
list1:[0, 1, 2, 3, 4, 5]
list2:[0, 2, 4, A, B, C]
5-
list1:[0, 1, 2, 3, 4, 5]
list2:[0, 2, 4, AA, B, C]
6-
list1.retainAll(list2):true
7-
list1:[0, 2, 4]
list2:[0, 2, 4, AA, B, C]
8-
list1:[0, 2, 4]
list2:[AA, B, C]
ArrayList에 저장된 객체의 삭제 과정
ArrayList저장된 세 번째 데이터(data[2])를 삭제하는 과정. list.remove(2);를 호출

⇒ 삽입 시는 반대로 하면 된다.
⇒ 마지막 데이터를 삭제하는 경우, 1의 과정(배열의 복사)은 필요없다.
⇒ 결국 1번의 이동과정(복사)가 부담이 되는과정 1번과정이 일어나지 않도록 하는 것이 좋다.
Java API 소스 보기
/jdk설치경로/src.zip


블로그의 정보
Hello 춘기's world
볼빵빵오춘기활동하기
Hello 춘기's world볼빵빵오춘기 님의 블로그입니다.