SlideShare a Scribd company logo
DataAnalysis
(Lecture 4 – MyArrayList)
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
https://www.youtube.com/channel/UC988e-Y8nto0LXVae0aqaOQ
목차
• ArrayList를 통한 실 세계 데이터 분석
• List 인터페이스와 호환가능한 배열기반 자료구조 설계
• MyArrayList를 이용한 데이터 분석 수행
2
I. ArrayList를 통한 실 세계 데이터 분석
1) 간단한 데이터 분석 수행
3
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
유튜브: https://youtu.be/duogQTvjDYI
I. ArrayList를 통한 실 세계 데이터 분석
1) 간단한 데이터 분석 수행
4
2) 이메일 이벤트를 보낸 사람 ID를 기반으로 오름차순 정렬
• 유튜브 - https://youtu.be/B_2ZzWE559Y
I. ArrayList를 통한 실 세계 데이터 분석
1) 간단한 데이터 분석 수행
5
3) 이메일 이벤트를 받는 사람 ID를 기반으로 내림차순 정렬
• 유튜브 - https://youtu.be/6ZPcNDMw0yE
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
1) ArrayList 복습
6
• 순서화 된 중복을 허용하는 Collection
• Collection 내의 Instance가 위치할 위치인 Index를 통해 조작
• Array를 기반으로 List Interface를 구현하며 Capacity (용적)이 동적으로 변화
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을 비움
<T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음
META
C
R
D
T
U
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
2) MyArrayList 설계 전략
7
• 공간효율만을 생각한 전략
• Object[] data를 유지 (data의 Capacity는 초기에 0임)
• add를 호출
• 기존의 Capacity보다 1개 큰 Array를 생성
• 기존의 data와 새로운 값을 추가 후 data로 유지
• remove를 호출
• 기존의 Capacity보다 1개 작은 Array를 생성
• 값이 지워진 data를 추가 후 data로 유지
myArrayList
K
0
myArrayList myArrayList
K
0
M
1
Add K Add M
newData
0
1
Create an array
with size -1
0
Initialize Object[] wi
th a size of 0
M
0
myArrayList
Remove
K
newData
M
0
2
Clone existing da
ta except K
newData
0 1
1
Create an array
with size + 1
newData
K
0 1
2
Clone existing da
ta
newData
K
0
M
1
3
Insert new data at
the end
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
3) MyArrayList 구현 준비
8
• List<E> 인터페이스를 구현한 MyArrayList<E> 생성
• Object[] data를 데이터 추상화 함
• 유튜브 - https://youtu.be/lmH32BlKKjY
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
9
• Object[] data가 length가 0이면 true
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():
비어있는지 확인
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
10
• Object[] data가 length가 0이면 true
• 유튜브: https://youtu.be/pMh2tDfu42A
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():
비어있는지 확인
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
11
• Object[] data의 length를 반환
• 유튜브: https://youtu.be/OpDiW5JBN6Y
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():
컬렉션의 크기 확인
``
``
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
12
Return Type Method Description
boolean add​(E e) Collection에 새로운 instance를 삽입
1. e를 추가하기 위해 data보다 크기가 한 개 큰 새로운 Object[] newData를 생성
2. 기존의 data를 newData에 복사
3. newData의 마지막 index에 e를 삽입
4. MyArrayList의 data를 newData로 갱신
유튜브: https://youtu.be/3wU37URl1EQ
K
0
M
1
O
2
O
3
myArrayList
newData
0 1 2 3 4
1
newData
K
0
M
1
O
2
O
3 4
2
newData
K
0
M
1
O
2
O
3
C
4
3
K
0
M
1
O
2
O
3
C
4
myArrayList
4
add(“C”):
Collection의 뒤에 C추가
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
13
1. e를 추가하기 위해 data보다 크기가 한 개 큰 새로운 Object[] newData를 생성
2. 기존의 data를 index전 까지 newData에 복사
3. newData[index]에 element SET
4. 나머지 data를 index 이후 부터 복사
5. MyArrayList의 data를 newData로 갱신
유튜브: https://youtu.be/Z0kcEQxSTlE
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
5
add(2,“O”):
인덱스 2위치에 O추가
(인덱스 2 이후의 요소는 뒤로 밀림)
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
14
• data 전체를 확인하여 equals 메소드를 이용하여 존재를 확인
• 유튜브- https://youtu.be/ghbg68wW2xY
Return Type Method Description
boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인
K
0
M
1
O
2
O
3
C
4
myArrayList
true
K
0
M
1
O
2
O
3
C
4
myArrayList
1 K equals C ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
2 M equals C ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
3 O equals C ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
4 O equals C ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
5 C equals C ? true
contains(“C”):
C가 포함되었는지 확인
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
15
• data[index]를 반환
• 유튜브 - https://youtu.be/4CpleXqXb8g
Return Type Method Description
E get(int index) List의 특정 위치에 있는 instance를 추출
K
0
M
1
O
2
O
3
C
4
myArrayList
M
get(1):
index 1의
instance 가져오기
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
16
• indexOf(Object o): data를 index 0부터 증가하는 방향으로 확인
• lastIndexOf(Object o): data를 index size-1부터 감소하는 방향으로 확인
• 유튜브 - https://youtu.be/5AV5n9XmJDU
Return Type Method Description
int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터)
int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터)
K
0
M
1
O
2
O
3
C
4
myArrayList
indexOf O 2
K
0
M
1
O
2
O
3
C
4
myArrayList
1 K equals O ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
2 M equals O ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
3 O equals O ? true
K
0
M
1
O
2
O
3
C
4
myArrayList
1 C equals O ? false
K
0
M
1
O
2
O
3
C
4
myArrayList
2 O equals O ? true
lastIndexOf O
3
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
17
• data[index] = element; 를 수행함
• 유튜브 - https://youtu.be/qEj4WfJlWbo
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
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
18
• o가 존재하지 않는다면, false를 반환한다.
• o가 존재한다면
• data보다 크기가 하나 작은 Object[] newData를 생성 후
• o 이전까지의 데이터와 o 이후의 데이터를 newData에 복사 후
• MyArrayList의 data가 newData가 되도록 함
• 유튜브 - https://youtu.be/rFHE67Lc3F0
Return Type Method Description
boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제
K
0
M
1
O
2
myArrayList
remove A
false
K
0
M
1
O
2
myArrayList
1 K equals A ?
false
K
0
M
1
O
2
myArrayList
2 M equals A ?
false
K
0
M
1
O
2
myArrayList
3 O equals A ?
false
true
remove M
newData
0 1
3
Create array
with size - 1
newData
K
0 1
4
Copy data
before M
newData
K
0
O
1
5
Copy data
after M
K
0
M
1
O
2
myArrayList
1 M equals A ?
false
K
0
M
1
O
2
myArrayList
2 M equals M ?
true
myArrayList
K
0
O
1
6
newData
becomes m
yArrayL
ist
data
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
19
• data보다 크기가 하나 작은 Object[] newData를 생성 후
• index 이전까지의 데이터와 index 이후의 데이터를 newData에 복사 후
• MyArrayList의 data가 newData가 되도록 함
• 유튜브 - https://youtu.be/PwYLhwEkUjE
Return Type Method Description
E remove(int index) List의 특정 위치에 있는 instance를 삭제
K
0
M
1
O
2
myArrayList M
0 1
newData
1 Create array
with size - 1
K
0 1
newData
2 Copy data
before index 1
K
0
O
1
newData
3 Copy data
after index 1
K
0
O
1
myArrayList
4
newData
becomes
myArrayList
data
remove(“M”):
M이 있다면 지우기
(앞에서 부터 검색, 단 한번 지움)
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
20
• Iterator는 data와 cursor를 유지하며, 각 메소드에 대해 cursor를 적절히 변화시켜 구현함
• 유튜브 - https://youtu.be/ZyWRwgBI3O0
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(): 다음 요소 (cursor+1)가 있으면 true 반환
• next(): 다음 요소를 반환하고 cursor를 다음으로 이동
Cursor의 초기위치: -1
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
true, then
next()
hasNext():
false, then
stop
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
21
• Iterator를 상속하는 기능성이 강화된 ListIterator를 구현하여 반환 함
• 유튜브 - https://youtu.be/uE6L1iOL08w
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)
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
true, then
previous()
hasPrevious():
false, then
stop
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
22
• Object[] data = new Object[]; 를 수행 함
• 유튜브 - https://youtu.be/z5ReppRZ1iY
Return Type Method Description
void clear() Collection을 비움
K
0
M
1
O
2
O
3
C
4
myArrayList
clear()
myArrayList
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
4) MyArrayList 구현
23
• data를 반환 함
• 유튜브 - https://youtu.be/HofZlCqSDgc
Return Type Method Description
<T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음
K
0
M
1
O
2
O
3
C
4
myArrayList
toArray
myArrayList
data in
newArray
Collection
K
0
M
1
O
2
O
3
C
4
newArray
String[]
0 1 2 3 4
newArray
String[]
II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현
5) MyArrayList를 이용한 데이터 분석 수행
24
1. 이벤트의 수 구하기
2. 최소 사람 ID를 구하기
3. 최대 사람 ID를 구하기
유튜브 - https://youtu.be/72xexdXpOOE

More Related Content

PDF
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
PDF
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
PDF
데이터 분석 3 - Java Collection Framework와 ArrayList
PDF
데이터 분석 2 - 동기부여
PDF
데이터 분석 1 - 소개
PDF
Collection framework
PPTX
05 컬렉션제너릭
PPTX
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 5 - Java Collection Framework - LinkedList 파헤치기
데이터 분석 3 - Java Collection Framework와 ArrayList
데이터 분석 2 - 동기부여
데이터 분석 1 - 소개
Collection framework
05 컬렉션제너릭
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업

What's hot (20)

PPTX
Python 활용: 이미지 처리와 데이터 분석
PDF
SpringCamp 2013 : About Jdk8
PPTX
빠르게 활용하는 파이썬3 스터디(ch1~4)
PPTX
Linq to object using c#
PPTX
Python programming for Bioinformatics
PDF
[Swift] Data Structure - Queue
PDF
알고리즘과 자료구조
PDF
Fp basic-kotlin
PDF
Scala
PDF
Python Programming: Function
PDF
JVM 메모리 해부학
PDF
Java collection
PPTX
영상 데이터의 처리와 정보의 추출
PPT
1.자료구조와 알고리즘(강의자료)
PDF
10장 문자열클래스와파일클래스
PDF
[Swift] Data Structure - Heap
PPTX
Just java
PDF
Tensorflow regression 텐서플로우 회귀
PPTX
TenforFlow Internals
PDF
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Python 활용: 이미지 처리와 데이터 분석
SpringCamp 2013 : About Jdk8
빠르게 활용하는 파이썬3 스터디(ch1~4)
Linq to object using c#
Python programming for Bioinformatics
[Swift] Data Structure - Queue
알고리즘과 자료구조
Fp basic-kotlin
Scala
Python Programming: Function
JVM 메모리 해부학
Java collection
영상 데이터의 처리와 정보의 추출
1.자료구조와 알고리즘(강의자료)
10장 문자열클래스와파일클래스
[Swift] Data Structure - Heap
Just java
Tensorflow regression 텐서플로우 회귀
TenforFlow Internals
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Ad

Similar to 데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자 (20)

PDF
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
PDF
집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking
PDF
Java_08 collection
PDF
Python + Excel
PPTX
02. data structure and stl
PPTX
파이썬 Collections 모듈 이해하기
PPTX
Firestore
PPT
강의자료3
PDF
[Algorithm] Counting Sort
PDF
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
PDF
파이썬 데이터과학 레벨2 - 데이터 시각화와 실전 데이터분석, 그리고 머신러닝 입문 (2020년 이태영)
PDF
Elastic Stack & Data pipeline (1장)
PDF
PDF
Java advancd ed10
PDF
5. queue
PDF
[Swift] Data Structure - Binary Search Tree
PDF
Data-Oriented Design과 유니티 DOTS
PDF
20140528 AWS Meister BlackBelt - Amazon Kinesis (Korean)
PDF
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
PDF
무엇이 무엇이 닮았을까?- OpenStack과 Azure
[SOPT] 데이터 구조 및 알고리즘 스터디 - #03 : 정렬 (기본, 효율, 초효율
집단 지성 (Programming collective intelligence) 스터디: Chapter 4 - Searching & Ranking
Java_08 collection
Python + Excel
02. data structure and stl
파이썬 Collections 모듈 이해하기
Firestore
강의자료3
[Algorithm] Counting Sort
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
파이썬 데이터과학 레벨2 - 데이터 시각화와 실전 데이터분석, 그리고 머신러닝 입문 (2020년 이태영)
Elastic Stack & Data pipeline (1장)
Java advancd ed10
5. queue
[Swift] Data Structure - Binary Search Tree
Data-Oriented Design과 유니티 DOTS
20140528 AWS Meister BlackBelt - Amazon Kinesis (Korean)
[SOPT] 데이터 구조 및 알고리즘 스터디 - #04 : 트리 기초, 이진 트리, 우선순위 큐
무엇이 무엇이 닮았을까?- OpenStack과 Azure
Ad

데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자

  • 1. DataAnalysis (Lecture 4 – MyArrayList) 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 https://www.youtube.com/channel/UC988e-Y8nto0LXVae0aqaOQ
  • 2. 목차 • ArrayList를 통한 실 세계 데이터 분석 • List 인터페이스와 호환가능한 배열기반 자료구조 설계 • MyArrayList를 이용한 데이터 분석 수행 2
  • 3. I. ArrayList를 통한 실 세계 데이터 분석 1) 간단한 데이터 분석 수행 3 1. 이벤트의 수 구하기 2. 최소 사람 ID를 구하기 3. 최대 사람 ID를 구하기 유튜브: https://youtu.be/duogQTvjDYI
  • 4. I. ArrayList를 통한 실 세계 데이터 분석 1) 간단한 데이터 분석 수행 4 2) 이메일 이벤트를 보낸 사람 ID를 기반으로 오름차순 정렬 • 유튜브 - https://youtu.be/B_2ZzWE559Y
  • 5. I. ArrayList를 통한 실 세계 데이터 분석 1) 간단한 데이터 분석 수행 5 3) 이메일 이벤트를 받는 사람 ID를 기반으로 내림차순 정렬 • 유튜브 - https://youtu.be/6ZPcNDMw0yE
  • 6. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 1) ArrayList 복습 6 • 순서화 된 중복을 허용하는 Collection • Collection 내의 Instance가 위치할 위치인 Index를 통해 조작 • Array를 기반으로 List Interface를 구현하며 Capacity (용적)이 동적으로 변화 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을 비움 <T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음 META C R D T U
  • 7. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 2) MyArrayList 설계 전략 7 • 공간효율만을 생각한 전략 • Object[] data를 유지 (data의 Capacity는 초기에 0임) • add를 호출 • 기존의 Capacity보다 1개 큰 Array를 생성 • 기존의 data와 새로운 값을 추가 후 data로 유지 • remove를 호출 • 기존의 Capacity보다 1개 작은 Array를 생성 • 값이 지워진 data를 추가 후 data로 유지 myArrayList K 0 myArrayList myArrayList K 0 M 1 Add K Add M newData 0 1 Create an array with size -1 0 Initialize Object[] wi th a size of 0 M 0 myArrayList Remove K newData M 0 2 Clone existing da ta except K newData 0 1 1 Create an array with size + 1 newData K 0 1 2 Clone existing da ta newData K 0 M 1 3 Insert new data at the end
  • 8. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 3) MyArrayList 구현 준비 8 • List<E> 인터페이스를 구현한 MyArrayList<E> 생성 • Object[] data를 데이터 추상화 함 • 유튜브 - https://youtu.be/lmH32BlKKjY
  • 9. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 9 • Object[] data가 length가 0이면 true 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(): 비어있는지 확인
  • 10. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 10 • Object[] data가 length가 0이면 true • 유튜브: https://youtu.be/pMh2tDfu42A 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(): 비어있는지 확인
  • 11. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 11 • Object[] data의 length를 반환 • 유튜브: https://youtu.be/OpDiW5JBN6Y 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(): 컬렉션의 크기 확인 `` ``
  • 12. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 12 Return Type Method Description boolean add​(E e) Collection에 새로운 instance를 삽입 1. e를 추가하기 위해 data보다 크기가 한 개 큰 새로운 Object[] newData를 생성 2. 기존의 data를 newData에 복사 3. newData의 마지막 index에 e를 삽입 4. MyArrayList의 data를 newData로 갱신 유튜브: https://youtu.be/3wU37URl1EQ K 0 M 1 O 2 O 3 myArrayList newData 0 1 2 3 4 1 newData K 0 M 1 O 2 O 3 4 2 newData K 0 M 1 O 2 O 3 C 4 3 K 0 M 1 O 2 O 3 C 4 myArrayList 4 add(“C”): Collection의 뒤에 C추가
  • 13. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 13 1. e를 추가하기 위해 data보다 크기가 한 개 큰 새로운 Object[] newData를 생성 2. 기존의 data를 index전 까지 newData에 복사 3. newData[index]에 element SET 4. 나머지 data를 index 이후 부터 복사 5. MyArrayList의 data를 newData로 갱신 유튜브: https://youtu.be/Z0kcEQxSTlE 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 5 add(2,“O”): 인덱스 2위치에 O추가 (인덱스 2 이후의 요소는 뒤로 밀림)
  • 14. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 14 • data 전체를 확인하여 equals 메소드를 이용하여 존재를 확인 • 유튜브- https://youtu.be/ghbg68wW2xY Return Type Method Description boolean contains​(Object o) Collection에 o라는 instance가 있는지 확인 K 0 M 1 O 2 O 3 C 4 myArrayList true K 0 M 1 O 2 O 3 C 4 myArrayList 1 K equals C ? false K 0 M 1 O 2 O 3 C 4 myArrayList 2 M equals C ? false K 0 M 1 O 2 O 3 C 4 myArrayList 3 O equals C ? false K 0 M 1 O 2 O 3 C 4 myArrayList 4 O equals C ? false K 0 M 1 O 2 O 3 C 4 myArrayList 5 C equals C ? true contains(“C”): C가 포함되었는지 확인
  • 15. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 15 • data[index]를 반환 • 유튜브 - https://youtu.be/4CpleXqXb8g Return Type Method Description E get(int index) List의 특정 위치에 있는 instance를 추출 K 0 M 1 O 2 O 3 C 4 myArrayList M get(1): index 1의 instance 가져오기
  • 16. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 16 • indexOf(Object o): data를 index 0부터 증가하는 방향으로 확인 • lastIndexOf(Object o): data를 index size-1부터 감소하는 방향으로 확인 • 유튜브 - https://youtu.be/5AV5n9XmJDU Return Type Method Description int indexOf(Object o) List에서 instance o의 위치를 찾기 (앞에서부터) int lastIndexOf(Object o) List에서 instance o의 위치를 찾기 (뒤에서부터) K 0 M 1 O 2 O 3 C 4 myArrayList indexOf O 2 K 0 M 1 O 2 O 3 C 4 myArrayList 1 K equals O ? false K 0 M 1 O 2 O 3 C 4 myArrayList 2 M equals O ? false K 0 M 1 O 2 O 3 C 4 myArrayList 3 O equals O ? true K 0 M 1 O 2 O 3 C 4 myArrayList 1 C equals O ? false K 0 M 1 O 2 O 3 C 4 myArrayList 2 O equals O ? true lastIndexOf O 3
  • 17. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 17 • data[index] = element; 를 수행함 • 유튜브 - https://youtu.be/qEj4WfJlWbo 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
  • 18. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 18 • o가 존재하지 않는다면, false를 반환한다. • o가 존재한다면 • data보다 크기가 하나 작은 Object[] newData를 생성 후 • o 이전까지의 데이터와 o 이후의 데이터를 newData에 복사 후 • MyArrayList의 data가 newData가 되도록 함 • 유튜브 - https://youtu.be/rFHE67Lc3F0 Return Type Method Description boolean remove​(Object o) Collection에 o라는 instance가 있다면 삭제 K 0 M 1 O 2 myArrayList remove A false K 0 M 1 O 2 myArrayList 1 K equals A ? false K 0 M 1 O 2 myArrayList 2 M equals A ? false K 0 M 1 O 2 myArrayList 3 O equals A ? false true remove M newData 0 1 3 Create array with size - 1 newData K 0 1 4 Copy data before M newData K 0 O 1 5 Copy data after M K 0 M 1 O 2 myArrayList 1 M equals A ? false K 0 M 1 O 2 myArrayList 2 M equals M ? true myArrayList K 0 O 1 6 newData becomes m yArrayL ist data
  • 19. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 19 • data보다 크기가 하나 작은 Object[] newData를 생성 후 • index 이전까지의 데이터와 index 이후의 데이터를 newData에 복사 후 • MyArrayList의 data가 newData가 되도록 함 • 유튜브 - https://youtu.be/PwYLhwEkUjE Return Type Method Description E remove(int index) List의 특정 위치에 있는 instance를 삭제 K 0 M 1 O 2 myArrayList M 0 1 newData 1 Create array with size - 1 K 0 1 newData 2 Copy data before index 1 K 0 O 1 newData 3 Copy data after index 1 K 0 O 1 myArrayList 4 newData becomes myArrayList data remove(“M”): M이 있다면 지우기 (앞에서 부터 검색, 단 한번 지움)
  • 20. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 20 • Iterator는 data와 cursor를 유지하며, 각 메소드에 대해 cursor를 적절히 변화시켜 구현함 • 유튜브 - https://youtu.be/ZyWRwgBI3O0 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(): 다음 요소 (cursor+1)가 있으면 true 반환 • next(): 다음 요소를 반환하고 cursor를 다음으로 이동 Cursor의 초기위치: -1 hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): true, then next() hasNext(): false, then stop
  • 21. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 21 • Iterator를 상속하는 기능성이 강화된 ListIterator를 구현하여 반환 함 • 유튜브 - https://youtu.be/uE6L1iOL08w 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) hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): true, then previous() hasPrevious(): false, then stop
  • 22. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 22 • Object[] data = new Object[]; 를 수행 함 • 유튜브 - https://youtu.be/z5ReppRZ1iY Return Type Method Description void clear() Collection을 비움 K 0 M 1 O 2 O 3 C 4 myArrayList clear() myArrayList
  • 23. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 4) MyArrayList 구현 23 • data를 반환 함 • 유튜브 - https://youtu.be/HofZlCqSDgc Return Type Method Description <T> T[] toArray​(T[] a) Collection을 T타입의 배열에 담음 K 0 M 1 O 2 O 3 C 4 myArrayList toArray myArrayList data in newArray Collection K 0 M 1 O 2 O 3 C 4 newArray String[] 0 1 2 3 4 newArray String[]
  • 24. II. List 인터페이스와 호환가능한 배열기반 자료구조 설계 및 구현 5) MyArrayList를 이용한 데이터 분석 수행 24 1. 이벤트의 수 구하기 2. 최소 사람 ID를 구하기 3. 최대 사람 ID를 구하기 유튜브 - https://youtu.be/72xexdXpOOE