SlideShare a Scribd company logo
DataAnalysis
(Lecture 3 – CollectionFramework andArrayList)
1
Jaewook Byun
Ph.D. Assistant Professor, Department of Software, Sejong University
Associate Director, Auto-ID Labs, Korea
jwbyun@sejong.ac.kr , bjw0829@gmail.com
https://sites.google.com/view/jack-dfpl/home
목차
• Java Collection Framework의 소개
• ArrayList의 소개
2
Java Collection Framework 소개
• Java Collection Framework
• Collection(컬렉션)
• 데이터의 모음
• Framework(프레임워크)
• 일반적인 기능성이 추상화된 소프트웨어
• Java Collection Framework
• 효율적인 데이터의 접근 조작이 가능하도록 데이터의 조직/관리/저장 방법을 추상화 해 놓은 것
• Class와 Interface로 구성
• java.util 패키지에 JDK 1.2 부터 제공
3
Head
8 1 9
6
Java Collection Framework 소개
• Java Collection Framework
• 계층구조
4
Collection
List Set
ArrayList
LinkedList
Queue
HashSet
TreeSet
Map
HashMap
TreeMap
Interface
Class
Java Collection Framework 소개
• Java Collection Framework
• 계층구조
• 유튜브 - https://youtu.be/ttPLc4g_sT0
5
java.util.ArrayList 소개
• Collection 소개
• 참조: https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html
• Java Collection Framework의 최상위 Interface
• java.util.Collection Interface은 instance의 모음에 대한 일반적인 연산을 정의
6
Collection
List Set
ArrayList
LinkedList
Queue
HashSet
TreeSet
Map
HashMap
TreeMap
Interface
Class
java.util.ArrayList 소개
• Collection 소개
• Java Documentation
• 유튜브 - https://youtu.be/yWEH8-ouvBY
7
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 구현은 다양한 성질을 가짐
• 중복을 허용 (예: ArrayList) vs. 중복을 허용하지 않음 (예: HashSet)
8
55 7 5
0 1 2 3 4
add 7
55 7 5 7
0 1 2 3 4
ArrayList
55
7
5
add 7 → 55
7
5
HashSet
`
`
`
`
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 구현은 다양한 성질을 가짐
• 순서를 유지 (예: TreeSet) vs. 순서가 없음 (예: HashSet)
9
Add C
TreeSet
A
B
D
add C
D
A
B
C
HashSet
`
`
A
B
D
A
B
D
C
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 구현은 다양한 성질을 가짐
• Thread-safe (예: Stack) vs. Thread-unsafe (예: LinkedList)
10
Stack LinkedList
Bom
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 구현은 다양한 성질을 가짐
• View Collections
• 특정 목적을 위해 Instance들을 직접 저장하지 않고, 원본 collection의 Wrapper Collection으
로써의 View를 제공
11
원본
List
Create
Read
Update
Delete
View에 대한 연산은 원본에 수행됨
return
List.subList
View
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 구현은 다양한 성질을 가짐
• Unmodifiable Collections
• 변경/조작에 관련된 연산이 불가능한 Collection
12
55 7 5
0 1 2
Unmodifiable Collection
Unsupported
Operation
Exception
throw
addall removeAll
retainAll
removeIf
clear
add remove
요청
Unmodifiable Collection에
사용 불가능한 연산들
java.util.ArrayList 소개
• Collection 소개
• Collection Interface의 연산
13
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add​(E e) Collection에 새로운 instance를 삽입
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
void clear() Collection을 비움
<T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음
Stream<E> stream() Collection에 대한 Stream을 반환
메타데이터
CREATE
RETRIEVE
DELETE
TRAVERSE
java.util.ArrayList 소개
• List 소개
• 순서화된 중복을 허용하는 Collection
• Collection 내의 Instance가 위치할 위치인 Index를 통해 조작
• 구현에 따라 제공하는 연산의 효율이 다를 수 있음
• 구현물: ArrayList, LinkedList, Stack 등
14
Collection
List
ArrayList
중복허용 순서 유지 Thread-Safe
O O ?
중복허용 순서 유지 Thread-Safe
O O X
LinkedList
중복허용 순서 유지 Thread-Safe
O O X
LinkedList
중복허용 순서 유지 Thread-Safe
O O O
java.util.ArrayList 소개
• List 소개
• 순서화된 중복을 허용하는 Collection
• Collection 내의 Instance가 위치할 위치인 Index를 통해 조작
15
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
int size() Collection의 크기를 반환
boolean add​(E e) Collection에 새로운 instance를 삽입
void add(int index, E element) List의 특정 위치에 instance를 삽입
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
E get(int index) List의 특정 위치에 있는 instance를 추출
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
E remove(int index) List의 특정 위치에 있는 instance를 삭제
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
void clear() Collection을 비움
Object[] toArray​() Collection을 배열에 담음
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
List<E> subList(int from, int to) List의 부분을 가져온다
Stream<E> stream() Collection에 대한 Stream을 반환
META
C
R
D
T
U
java.util.ArrayList 소개
• ArrayList 소개
• Array를 기반으로 List Interface를 구현한 것
• Capacity (용적)이 변경 가능한 Array
• Capacity
• 기본값 10
• 특정 Threshold를 초과할 때 자동으로 증가
• 수동으로도 증가 가능: ensureCapacity
• 현재 ArrayList의 실제 크기로 Capacity를 줄일 수 있음: trimToSize
16
Collection
List
ArrayList
S E J O N G - M O O
0 1 2 3 4 5 6 7 8 9
Add C
S E J O N G - M O O C
0 1 2 3 4 5 6 7 8 9 10 11 …
Trim to size 11
S E J O N G - M O O C
0 1 2 3 4 5 6 7 8 9 10
java.util.ArrayList 소개
• ArrayList 소개
• 유튜브 - https://youtu.be/5dt1rAaqSXU
17
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/l_bvd_e25qk
18
Return Type Method Description
생성자 ArrayList() 빈 ArrayList를 생성
JVM Heap
ArrayList
[ ]
1. 빈 리스트 생성
Integer instance들을
담을 수 있음
ArrayList<Integer> linkedList = new ArrayList<Integer>();
2. 주소할당
Generic E는 ArrayList의
구성요소의 타입
new ArrayList<E>();
Generic E를
Integer로 설정하여 생성
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/8ILu6UW2-Rk
19
Return Type Method Description
boolean isEmpty() Collection이 비어 있는지 확인
isEmpty():
비어있는지 확인
true
false
myArrayList
Data length is 0
myArrayList
K
0
M
1
Data length is 2
isEmpty():
비어있는지 확인
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/R7LVXj_JmdU
20
Return Type Method Description
int size() Collection의 크기를 반환
size():
컬렉션의 크기 확인
0
myArrayList
Data length is 0
myArrayList
K
0
M
1
Data length is 2
2
size():
컬렉션의 크기 확인
``
``
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/V_0IWM3lhrg
21
K
0
M
1
O
2
O
3
myArrayList
K
0
M
1
O
2
O
3
C
4
myArrayList
add(“C”):
Collection의 뒤에 C추가
Return Type Method Description
boolean add​(E e) Collection에 새로운 instance를 삽입
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/1Znx4lLMRbw
22
Return Type Method Description
void add(int index, E element) List의 특정 위치에 instance를 삽입
K
0
M
1
O
2
C
3
myArrayList
K
0
M
1
O
2
O
3
C
4
myArrayList
add(2,“O”):
인덱스 2위치에 O추가
(인덱스 2 이후의 요소는 뒤로 밀림)
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/-loq0cJFqlA
23
Return Type Method Description
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
K
0
M
1
O
2
O
3
C
4
myArrayList
contains(“C”):
C가 포함되었는지 확인
true
K
0
M
1
O
2
O
3
C
4
myArrayList
contains(“P”):
P가 포함되었는지 확인
false
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/dhHsXSRKrQc
24
Return Type Method Description
E get(int index) List의 특정 위치에 있는 instance를 추출
K
0
M
1
O
2
O
3
C
4
myArrayList
get(1):
index 1의
instance 가져오기
M
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/eiA5DpY-H4c
25
K
0
M
1
O
2
O
3
C
4
myArrayList
3
indexOf(“o”): O가 최초로 발견된 인덱스 (뒤에서부터 찾기)
2
lastIndexOf(“o”): O가 최초로 발견된 인덱스 (앞에서부터 찾기)
Return Type Method Description
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/_uxTGHg-GOA
26
Return Type Method Description
E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트
K
0
N
1
O
2
O
3
C
4
myArrayList
set(1, “M”):
인덱스 1의 요소를 M으로 갱신
K
0
M
1
O
2
O
3
C
4
myArrayList
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/ICqVzqhyl4w
27
Return Type Method Description
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
K
0
M
1
O
2
O
3
C
4
myArrayList
remove(“C”):
C가 있다면 지우기
(앞에서 부터 검색, 단 한번 지움)
K
0
M
1
O
2
O
3
C
4
myArrayList
C
5
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/8RdO04uvQiQ
28
Return Type Method Description
E remove(int index) List의 특정 위치에 있는 instance를 삭제
K
0
M
1
O
2
O
3
C
4
myArrayList
remove(4):
인덱스 4 instance 삭제
(4이후의 값이 앞당겨짐)
K
0
M
1
O
2
O
3
C
4
myArrayList
C
5
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/sCFPZ4PWQR4
29
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
값 K M O O C
Index 0 1 2 3 4
Iterator
• Collection을 순회할 수 있는 것
• Cursor가 첫 instance 이전에 위치
• hasNext(), next() 연산 제공
Cursor의 초기위치: -1
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/sCFPZ4PWQR4
30
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
값 K M O O C
Index 0 1 2 3 4
Iterator
• hasNext(): 다음 요소 (cursor+1)가 있으면 true 반환
• next(): 다음 요소를 반환하고 cursor를 다음으로 이동
1. hasNext(): true
index 0에는 K가 있음
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/sCFPZ4PWQR4
31
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
값 K M O O C
Index 0 1 2 3 4
Iterator
• hasNext(): 다음 요소 (cursor+1)가 있으면 true 반환
• next(): 다음 요소를 반환하고 cursor를 다음으로 이동
2. next(): K
K를 반환하고 index 0으로 Cursor 이동
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/sCFPZ4PWQR4
32
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
값 K M O O C
Index 0 1 2 3 4
Iterator
• hasNext(), next()를 반복 수행하여 전체 순회가능
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
false, then
stop
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 (추가영상) - https://youtu.be/y2Bq7IcQhlM
33
Return Type Method Description
Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/HI8ZFHoezcQ
34
Return Type Method Description
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
값 K M O O C
Index 0 1 2 3 4
ListIterator(5)
리스트의 size()
• List를 순회할 수 있는 Iterator
• 생성자 ListIterator(int index)를 통해 초기 위치 설정가능
• Iterator()는 ListIterator(0)과 동일
• Cursor는 index -1에 위치
• hasNext(), next(), hasPrevious(), previous() 연산 제공
Cursor의 초기위치: 4 (5-1)
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/HI8ZFHoezcQ
35
Return Type Method Description
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
값 K M O O C
Index 0 1 2 3 4
ListIterator(5)
리스트의 size()
• hasPrevious(): 이전 요소 (cursor)가 있으면 true 반환
• previous(): 이전 요소를 반환하고 cursor를 이전으로 이동
1. hasPrevious(): true
index 4에는 C가 있음
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/HI8ZFHoezcQ
36
Return Type Method Description
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
값 K M O O C
Index 0 1 2 3 4
ListIterator(5)
리스트의 size()
• hasPrevious(): 이전 요소 (cursor)가 있으면 true 반환
• previous(): 이전 요소를 반환하고 cursor를 이전으로 이동
2. previous(): C
C를 반환하고 index 3으로 Cursor 이동
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 - https://youtu.be/HI8ZFHoezcQ
37
Return Type Method Description
ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환
값 K M O O C
Index 0 1 2 3 4
ListIterator(5)
리스트의 size()
• hasPrevious(), previous()를 반복 수행하여
전체 순회가능
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
false, then
stop
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 – https://youtu.be/9iabEekbuhA
38
Return Type Method Description
void clear() Collection을 비움
K
0
M
1
O
2
O
3
C
4
myArrayList
clear():
Collection을 비움
myArrayList
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 – https://youtu.be/f-4DfcIidac
39
Return Type Method Description
<T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음
K
0
M
1
O
2
O
3
C
4
myArrayList
toArray(newArray):
myArrayList를
newArray에 담는다.
Collection
K
0
M
1
O
2
O
3
C
4
newArray
String[]
0 1 2 3 4
newArray
String[]
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 – https://youtu.be/W1q_K_Km-E4
40
Return Type Method Description
void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
java.util.ArrayList CRUD
• ArrayList CRUD
• 유튜브 – https://youtu.be/Nk5RPrR57YE
41
Return Type Method Description
List<E> subList(int from, int to) List의 부분을 가져온다

More Related Content

PDF
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
PDF
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
PDF
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
PDF
데이터 분석 2 - 동기부여
PDF
Collection framework
PPTX
05 컬렉션제너릭
PDF
데이터 분석 1 - 소개
PDF
SpringCamp 2013 : About Jdk8
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 2 - 동기부여
Collection framework
05 컬렉션제너릭
데이터 분석 1 - 소개
SpringCamp 2013 : About Jdk8

What's hot (20)

PDF
Fp basic-kotlin
PPTX
Linq to object using c#
PPTX
빠르게 활용하는 파이썬3 스터디(ch1~4)
PDF
Scala
PPTX
Python 활용: 이미지 처리와 데이터 분석
PDF
알고리즘과 자료구조
PPTX
Python programming for Bioinformatics
PDF
Python Programming: Function
PPTX
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
PPTX
Just java
PDF
10장 문자열클래스와파일클래스
PDF
[Swift] Data Structure - Queue
PDF
JVM 메모리 해부학
PDF
씹고 뜯고 맛보고 즐기는 스트림 API
PPT
1.자료구조와 알고리즘(강의자료)
PPTX
Valentine
PDF
자바8 람다 나머지 공개
PPTX
종이접기(fold) 프로그래밍
PPTX
R 프로그래밍-향상된 데이타 조작
PDF
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Fp basic-kotlin
Linq to object using c#
빠르게 활용하는 파이썬3 스터디(ch1~4)
Scala
Python 활용: 이미지 처리와 데이터 분석
알고리즘과 자료구조
Python programming for Bioinformatics
Python Programming: Function
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
Just java
10장 문자열클래스와파일클래스
[Swift] Data Structure - Queue
JVM 메모리 해부학
씹고 뜯고 맛보고 즐기는 스트림 API
1.자료구조와 알고리즘(강의자료)
Valentine
자바8 람다 나머지 공개
종이접기(fold) 프로그래밍
R 프로그래밍-향상된 데이타 조작
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Ad

Similar to 데이터 분석 3 - Java Collection Framework와 ArrayList (20)

PDF
Java collection
PDF
Java_08 collection
PPTX
파이썬 Collections 모듈 이해하기
PDF
Java(4/4)
PDF
C++ Advanced 강의 4주차
PDF
[Swift] Data Structure - Dequeue
PDF
[Swift] Iterator
PPT
강의자료3
PDF
자료구조 큐
PDF
Python + Excel
PPTX
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
PPTX
R 기본-데이타형 소개
PDF
Swift3 subscript inheritance initialization
PPTX
코딩테스트 합격자 되기 C++ 04_05장_코딩 테스트를 할때 반드시 알아야할 문법
PDF
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
PDF
[TECHCON 2019: MOBILE - Android]2.예제에서는 알려주지 않는 Model 이야기
PDF
5. queue
PDF
자바 테스트 자동화
PDF
[Algorithm] Counting Sort
PDF
Java stream v0.1
Java collection
Java_08 collection
파이썬 Collections 모듈 이해하기
Java(4/4)
C++ Advanced 강의 4주차
[Swift] Data Structure - Dequeue
[Swift] Iterator
강의자료3
자료구조 큐
Python + Excel
[GPG 스터디] 1.4 게임프로그래밍에서의 STL 활용
R 기본-데이타형 소개
Swift3 subscript inheritance initialization
코딩테스트 합격자 되기 C++ 04_05장_코딩 테스트를 할때 반드시 알아야할 문법
실용주의 디자인패턴 2 인터페이스로 프로그래밍하기
[TECHCON 2019: MOBILE - Android]2.예제에서는 알려주지 않는 Model 이야기
5. queue
자바 테스트 자동화
[Algorithm] Counting Sort
Java stream v0.1
Ad

데이터 분석 3 - Java Collection Framework와 ArrayList

  • 1. DataAnalysis (Lecture 3 – CollectionFramework andArrayList) 1 Jaewook Byun Ph.D. Assistant Professor, Department of Software, Sejong University Associate Director, Auto-ID Labs, Korea [email protected] , [email protected] https://sites.google.com/view/jack-dfpl/home
  • 2. 목차 • Java Collection Framework의 소개 • ArrayList의 소개 2
  • 3. Java Collection Framework 소개 • Java Collection Framework • Collection(컬렉션) • 데이터의 모음 • Framework(프레임워크) • 일반적인 기능성이 추상화된 소프트웨어 • Java Collection Framework • 효율적인 데이터의 접근 조작이 가능하도록 데이터의 조직/관리/저장 방법을 추상화 해 놓은 것 • Class와 Interface로 구성 • java.util 패키지에 JDK 1.2 부터 제공 3 Head 8 1 9 6
  • 4. Java Collection Framework 소개 • Java Collection Framework • 계층구조 4 Collection List Set ArrayList LinkedList Queue HashSet TreeSet Map HashMap TreeMap Interface Class
  • 5. Java Collection Framework 소개 • Java Collection Framework • 계층구조 • 유튜브 - https://youtu.be/ttPLc4g_sT0 5
  • 6. java.util.ArrayList 소개 • Collection 소개 • 참조: https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Collection.html • Java Collection Framework의 최상위 Interface • java.util.Collection Interface은 instance의 모음에 대한 일반적인 연산을 정의 6 Collection List Set ArrayList LinkedList Queue HashSet TreeSet Map HashMap TreeMap Interface Class
  • 7. java.util.ArrayList 소개 • Collection 소개 • Java Documentation • 유튜브 - https://youtu.be/yWEH8-ouvBY 7
  • 8. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 구현은 다양한 성질을 가짐 • 중복을 허용 (예: ArrayList) vs. 중복을 허용하지 않음 (예: HashSet) 8 55 7 5 0 1 2 3 4 add 7 55 7 5 7 0 1 2 3 4 ArrayList 55 7 5 add 7 → 55 7 5 HashSet ` ` ` `
  • 9. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 구현은 다양한 성질을 가짐 • 순서를 유지 (예: TreeSet) vs. 순서가 없음 (예: HashSet) 9 Add C TreeSet A B D add C D A B C HashSet ` ` A B D A B D C
  • 10. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 구현은 다양한 성질을 가짐 • Thread-safe (예: Stack) vs. Thread-unsafe (예: LinkedList) 10 Stack LinkedList Bom
  • 11. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 구현은 다양한 성질을 가짐 • View Collections • 특정 목적을 위해 Instance들을 직접 저장하지 않고, 원본 collection의 Wrapper Collection으 로써의 View를 제공 11 원본 List Create Read Update Delete View에 대한 연산은 원본에 수행됨 return List.subList View
  • 12. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 구현은 다양한 성질을 가짐 • Unmodifiable Collections • 변경/조작에 관련된 연산이 불가능한 Collection 12 55 7 5 0 1 2 Unmodifiable Collection Unsupported Operation Exception throw addall removeAll retainAll removeIf clear add remove 요청 Unmodifiable Collection에 사용 불가능한 연산들
  • 13. java.util.ArrayList 소개 • Collection 소개 • Collection Interface의 연산 13 Return Type Method Description boolean isEmpty() Collection이 비어 있는지 확인 int size() Collection의 크기를 반환 boolean add​(E e) Collection에 새로운 instance를 삽입 boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 void clear() Collection을 비움 <T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음 Stream<E> stream() Collection에 대한 Stream을 반환 메타데이터 CREATE RETRIEVE DELETE TRAVERSE
  • 14. java.util.ArrayList 소개 • List 소개 • 순서화된 중복을 허용하는 Collection • Collection 내의 Instance가 위치할 위치인 Index를 통해 조작 • 구현에 따라 제공하는 연산의 효율이 다를 수 있음 • 구현물: ArrayList, LinkedList, Stack 등 14 Collection List ArrayList 중복허용 순서 유지 Thread-Safe O O ? 중복허용 순서 유지 Thread-Safe O O X LinkedList 중복허용 순서 유지 Thread-Safe O O X LinkedList 중복허용 순서 유지 Thread-Safe O O O
  • 15. java.util.ArrayList 소개 • List 소개 • 순서화된 중복을 허용하는 Collection • Collection 내의 Instance가 위치할 위치인 Index를 통해 조작 15 Return Type Method Description boolean isEmpty() Collection이 비어 있는지 확인 int size() Collection의 크기를 반환 boolean add​(E e) Collection에 새로운 instance를 삽입 void add(int index, E element) List의 특정 위치에 instance를 삽입 boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 E get(int index) List의 특정 위치에 있는 instance를 추출 int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터) int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터) E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트 boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 E remove(int index) List의 특정 위치에 있는 instance를 삭제 Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 void clear() Collection을 비움 Object[] toArray​() Collection을 배열에 담음 void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함 List<E> subList(int from, int to) List의 부분을 가져온다 Stream<E> stream() Collection에 대한 Stream을 반환 META C R D T U
  • 16. java.util.ArrayList 소개 • ArrayList 소개 • Array를 기반으로 List Interface를 구현한 것 • Capacity (용적)이 변경 가능한 Array • Capacity • 기본값 10 • 특정 Threshold를 초과할 때 자동으로 증가 • 수동으로도 증가 가능: ensureCapacity • 현재 ArrayList의 실제 크기로 Capacity를 줄일 수 있음: trimToSize 16 Collection List ArrayList S E J O N G - M O O 0 1 2 3 4 5 6 7 8 9 Add C S E J O N G - M O O C 0 1 2 3 4 5 6 7 8 9 10 11 … Trim to size 11 S E J O N G - M O O C 0 1 2 3 4 5 6 7 8 9 10
  • 17. java.util.ArrayList 소개 • ArrayList 소개 • 유튜브 - https://youtu.be/5dt1rAaqSXU 17
  • 18. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/l_bvd_e25qk 18 Return Type Method Description 생성자 ArrayList() 빈 ArrayList를 생성 JVM Heap ArrayList [ ] 1. 빈 리스트 생성 Integer instance들을 담을 수 있음 ArrayList<Integer> linkedList = new ArrayList<Integer>(); 2. 주소할당 Generic E는 ArrayList의 구성요소의 타입 new ArrayList<E>(); Generic E를 Integer로 설정하여 생성
  • 19. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/8ILu6UW2-Rk 19 Return Type Method Description boolean isEmpty() Collection이 비어 있는지 확인 isEmpty(): 비어있는지 확인 true false myArrayList Data length is 0 myArrayList K 0 M 1 Data length is 2 isEmpty(): 비어있는지 확인
  • 20. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/R7LVXj_JmdU 20 Return Type Method Description int size() Collection의 크기를 반환 size(): 컬렉션의 크기 확인 0 myArrayList Data length is 0 myArrayList K 0 M 1 Data length is 2 2 size(): 컬렉션의 크기 확인 `` ``
  • 21. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/V_0IWM3lhrg 21 K 0 M 1 O 2 O 3 myArrayList K 0 M 1 O 2 O 3 C 4 myArrayList add(“C”): Collection의 뒤에 C추가 Return Type Method Description boolean add​(E e) Collection에 새로운 instance를 삽입
  • 22. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/1Znx4lLMRbw 22 Return Type Method Description void add(int index, E element) List의 특정 위치에 instance를 삽입 K 0 M 1 O 2 C 3 myArrayList K 0 M 1 O 2 O 3 C 4 myArrayList add(2,“O”): 인덱스 2위치에 O추가 (인덱스 2 이후의 요소는 뒤로 밀림)
  • 23. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/-loq0cJFqlA 23 Return Type Method Description boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 K 0 M 1 O 2 O 3 C 4 myArrayList contains(“C”): C가 포함되었는지 확인 true K 0 M 1 O 2 O 3 C 4 myArrayList contains(“P”): P가 포함되었는지 확인 false
  • 24. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/dhHsXSRKrQc 24 Return Type Method Description E get(int index) List의 특정 위치에 있는 instance를 추출 K 0 M 1 O 2 O 3 C 4 myArrayList get(1): index 1의 instance 가져오기 M
  • 25. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/eiA5DpY-H4c 25 K 0 M 1 O 2 O 3 C 4 myArrayList 3 indexOf(“o”): O가 최초로 발견된 인덱스 (뒤에서부터 찾기) 2 lastIndexOf(“o”): O가 최초로 발견된 인덱스 (앞에서부터 찾기) Return Type Method Description int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터) int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
  • 26. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/_uxTGHg-GOA 26 Return Type Method Description E set(int index, E element) List의 특정 위치의 instance 값을 element로 업데이트 K 0 N 1 O 2 O 3 C 4 myArrayList set(1, “M”): 인덱스 1의 요소를 M으로 갱신 K 0 M 1 O 2 O 3 C 4 myArrayList
  • 27. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/ICqVzqhyl4w 27 Return Type Method Description boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 K 0 M 1 O 2 O 3 C 4 myArrayList remove(“C”): C가 있다면 지우기 (앞에서 부터 검색, 단 한번 지움) K 0 M 1 O 2 O 3 C 4 myArrayList C 5
  • 28. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/8RdO04uvQiQ 28 Return Type Method Description E remove(int index) List의 특정 위치에 있는 instance를 삭제 K 0 M 1 O 2 O 3 C 4 myArrayList remove(4): 인덱스 4 instance 삭제 (4이후의 값이 앞당겨짐) K 0 M 1 O 2 O 3 C 4 myArrayList C 5
  • 29. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/sCFPZ4PWQR4 29 Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 값 K M O O C Index 0 1 2 3 4 Iterator • Collection을 순회할 수 있는 것 • Cursor가 첫 instance 이전에 위치 • hasNext(), next() 연산 제공 Cursor의 초기위치: -1
  • 30. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/sCFPZ4PWQR4 30 Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 값 K M O O C Index 0 1 2 3 4 Iterator • hasNext(): 다음 요소 (cursor+1)가 있으면 true 반환 • next(): 다음 요소를 반환하고 cursor를 다음으로 이동 1. hasNext(): true index 0에는 K가 있음
  • 31. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/sCFPZ4PWQR4 31 Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 값 K M O O C Index 0 1 2 3 4 Iterator • hasNext(): 다음 요소 (cursor+1)가 있으면 true 반환 • next(): 다음 요소를 반환하고 cursor를 다음으로 이동 2. next(): K K를 반환하고 index 0으로 Cursor 이동
  • 32. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/sCFPZ4PWQR4 32 Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환 값 K M O O C Index 0 1 2 3 4 Iterator • hasNext(), next()를 반복 수행하여 전체 순회가능 hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): false, then stop
  • 33. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 (추가영상) - https://youtu.be/y2Bq7IcQhlM 33 Return Type Method Description Iterator<E> iterator() Collection을 순회할 수 있는 iterator를 반환
  • 34. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/HI8ZFHoezcQ 34 Return Type Method Description ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 값 K M O O C Index 0 1 2 3 4 ListIterator(5) 리스트의 size() • List를 순회할 수 있는 Iterator • 생성자 ListIterator(int index)를 통해 초기 위치 설정가능 • Iterator()는 ListIterator(0)과 동일 • Cursor는 index -1에 위치 • hasNext(), next(), hasPrevious(), previous() 연산 제공 Cursor의 초기위치: 4 (5-1)
  • 35. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/HI8ZFHoezcQ 35 Return Type Method Description ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 값 K M O O C Index 0 1 2 3 4 ListIterator(5) 리스트의 size() • hasPrevious(): 이전 요소 (cursor)가 있으면 true 반환 • previous(): 이전 요소를 반환하고 cursor를 이전으로 이동 1. hasPrevious(): true index 4에는 C가 있음
  • 36. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/HI8ZFHoezcQ 36 Return Type Method Description ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 값 K M O O C Index 0 1 2 3 4 ListIterator(5) 리스트의 size() • hasPrevious(): 이전 요소 (cursor)가 있으면 true 반환 • previous(): 이전 요소를 반환하고 cursor를 이전으로 이동 2. previous(): C C를 반환하고 index 3으로 Cursor 이동
  • 37. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 - https://youtu.be/HI8ZFHoezcQ 37 Return Type Method Description ListIterator<E> listIterator(int index) List를 순회할 수 있는 listIterator를 반환 값 K M O O C Index 0 1 2 3 4 ListIterator(5) 리스트의 size() • hasPrevious(), previous()를 반복 수행하여 전체 순회가능 hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): false, then stop
  • 38. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 – https://youtu.be/9iabEekbuhA 38 Return Type Method Description void clear() Collection을 비움 K 0 M 1 O 2 O 3 C 4 myArrayList clear(): Collection을 비움 myArrayList
  • 39. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 – https://youtu.be/f-4DfcIidac 39 Return Type Method Description <T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음 K 0 M 1 O 2 O 3 C 4 myArrayList toArray(newArray): myArrayList를 newArray에 담는다. Collection K 0 M 1 O 2 O 3 C 4 newArray String[] 0 1 2 3 4 newArray String[]
  • 40. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 – https://youtu.be/W1q_K_Km-E4 40 Return Type Method Description void sort(Comparator<? super E> comparator) List를 특정 비교방법에 의해 정렬함
  • 41. java.util.ArrayList CRUD • ArrayList CRUD • 유튜브 – https://youtu.be/Nk5RPrR57YE 41 Return Type Method Description List<E> subList(int from, int to) List의 부분을 가져온다