[Java] List_ArrayList(Collection Framework)
배열
데이터 삽입, 삭제 시 불필요한 연산 작업이 수행되어많은 데이터를 처리하는 데 문제점이 있다.
배열의 크기가 정해지면 변경되지 않는다.
Collection FrameWork
배열의 문제점 해결을 위해 Collection Framework를 제공
List
|
순서가 있는 데이터의 집합, 데이터 중복 O
|
Set
|
순서를 유지하지 않는 데이터의 집합, 데이터 중복 X
|
Map
|
키(key)와 값(value)의 쌍(pair)으로 이루어진 데이터의 집합,
순서는 유지되지 않고 key는 데이터 중복 X, value는 데이터 중복 O |
List (ArrayList, Vector, LinkedList)
ArrayList
: 기존 배열 선언, 사용 방식의 단점을 없애고 객체 배열을 효율적으로 관리해주는 클래스
공간이 차면 자동으로 늘어나기도 한다.
ArrayList<> 참조변수 = new ArrayList<>();
ArryaList<Integer> num = new ArrayList<>();
ArrayList num = new ArrayList();
< > : 제너릭
어떤 객체를 넣을지 지정
메소드
add : 데이터 추가할 때 문제 있음 오류
offer : 데이터 추가 잘 되면 true 안 되면 false 리턴하고 오류 발생은 안 해
peek : 데이터 뽑아내고 그대로 유지
poll : 데이터 뽑아내고 삭제됨
push : 첫 번째 데이터에 집어넣어
pop : 첫 번째 데이터 뽑아내
toArray() : 컬렉션을 일반 배열로 변환할 때 사용
add, addAll : 객체 추가
clear : 컬렉션의 모든 객체 삭제
contains, containsAll : 포함된 것만 남김
equals : 동일한 컬렉션인지 비교
hashCode : 컬렉션의 hashCode 반환
isEmpty : 컬렉션이 비었는지 확인
iterator : 컬렉션의 요소를 차례차례 꺼내서 읽어옴
remove, removeAll : 삭제
retainAll : 교집합만 남김
size : 요소 수 반환
toArray : 객체를 객체 배열로 반환
add( )
: 데이터 추가
num.add(1);
num.add(14);
size( )
: 요소 개수 반환
public class Collection {
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
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));
System.out.println(list1.size());
}
}
// 6
get(int index)
: index에 위치한 요소 값 반환
public class Collection {
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
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));
System.out.println(list1.get(2));
System.out.println(list1.get(4));
}
}
// 2
// 1
remove(int index)
: 요소 값 제거하고 그 값을 반환
public class Collection {
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
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));
System.out.println(list1.remove(2));
System.out.println(list1.remove(4));
System.out.println(list1);
}
}
// 2
// 3
// [5, 4, 0, 1]
isEmpty( )
: 배열 비었는지 확인하고 true, false로 반환
public class Collection {
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
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();
System.out.println(list1.isEmpty());
System.out.println(list2.isEmpty());
}
}
// false
// true
list1.retainAll(list2)
: list2에 있는 것만 남기고 다 삭제
list1.removeAll(list2)
: list2와 겹치는 것 다 삭제
Iterator()
: 커서를 사용해 전체 데이터 하나하나 접근(반복)
Iterator<String? iter = list.iterator();
while(iter.hasNext()) { //hasNext() 커서 다음에 데이터 있는지?
System.out.println(iter.next()); // Iterator 값 출력
}
참고!!!!!
Iterator은 접근하는 도중에 데이터가 삭제되면 오류 발생하지만
Enumeration은 도중에 삭제되어도 다음 데이터에 접근 가능하다.
list1.containsAll(list2)
:list1에 list2가 포함되어있는지 반환
예시
public class Collection {
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
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));
print(list1, list2);
// subList() : index 1부터 4 전까지 = 1,2,3
Collections.sort(list1); // 정렬
Collections.sort(list2); // 정렬
print(list1, list2);
// list2는 list1에 모두 포함되어있는지?
System.out.println("list1.containsAll(list2):" + list1.containsAll(list2));
list2.add("B");
list2.add("C");
list2.add("A");
print(list1, list2);
list2.set(3, "AA");
print(list1, list2);
//포함된 것만 남겨
System.out.println("list1.retainAll(list2):" + list1.retainAll(list2));
print(list1, list2);
for (int i = list2.size(); i >=0; i--) {
if(list1.contains(list2.get(i))){
list2.remove(i);
}
}
}
static void print(ArrayList list1, ArrayList list2) {
System.out.println("list1: " + list1);
System.out.println("list2: " + list2);
}
}
// list1: [5, 4, 2, 0, 1, 3]
// list2: [4, 2, 0]
// list1: [0, 1, 2, 3, 4, 5]
// list2: [0, 2, 4]
// list1.containsAll(list2):true
// list1: [0, 1, 2, 3, 4, 5]
// list2: [0, 2, 4, B, C, A]
// list1: [0, 1, 2, 3, 4, 5]
// list2: [0, 2, 4, AA, C, A]
// list1.retainAll(list2):true
// list1: [0, 2, 4]
// list2: [0, 2, 4, AA, C, A]