SlideShare a Scribd company logo
SWIFT
Data Structure -
Array
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Array List
•Concept
•Features
•Implementation
•References
Array List
자료구조에서 가장 기본이 되는 구조는 배열(리스트)입니다.
리스트는 크게 아래와 같이 2개의 리스트로 구분할 수 있습니다.
순차 리스트 :
배열을 기반으로 구현된 리스트
연결 리스트 :
메모리의 동적 할당을 기반으로 구현된 리스트
본 강의에서는 Swift 코드를 활용하여 순차 리스트에 대해서 살펴
봅니다.
Concept
순차리스트는 아래와 같이 특정 자료를 정해진 공간안에서 순차적
으로 저장 및 가져오는 자료구조입니다.
Features
순차 리스트(Array)의 특징을 살펴보면 아래와 같습니다.
- 데이터를 순차적으로 저장합니다.
- 데이터 중복 저장을 허용합니다.
- 배열의 총 길이는 초기에 결정되어야 합니다.(변경 불가)
- 인덱스 값을 기준으로 데이터의 참조가 가능합니다.
- 삭제의 과정에서 데이터의 이동이 빈번하게 일어납니다.
Implementation
Swift를 활용하여 Array 를 구현해보겠습니다.
우선 필요한 메소드는 아래와 같습니다.
- init() : 배열의 크기를 설정하는 초기화 함수
- insert() : 데이터 입력
- remove() : 특정 인덱스의 데이터 삭제
- removeAll() : 모든 데이터 삭제
- get() : 특정 인덱스의 데이터 값을 반환
- count() : 현재 배열의 크기를 반환
Implementation
가장 데이터의 기본이 되는 Node 클래스 입니다.
해당 Node 클래스는 모든 데이터 형식을 받을 수 있도록
Generic 형태로 구현이 되어 있습니다.
class Node<T> {
fileprivate var data:T
init(_ data: T) {
self.data = data
}
}
Implementation
class ArrayList<T> {
private var size:Int
private var list:[Node<T>]
init(size:Int) {
self.list = [Node<T>]()
self.size = size
}
func insert(item:T) {
if self.list.count < size {
self.list.append(Node<T>(item))
}
}
func remove(at:Int) {
if self.list.count <= 0 { return }
if self.list.count <= at { return }
self.list.remove(at: at)
}
func removeAll() {
self.list.removeAll()
}
func get(index:Int) -> T {
return self.list[index].data
}
func count() -> Int {
size = list.count
return size
}
}
Implementation
아래와 같이 사용이 가능합니다.
let list:ArrayList<Int> = ArrayList(size: 5)
list.insert(item: 1)
list.insert(item: 2)
list.insert(item: 3)
list.insert(item: 4)
list.insert(item: 5)
// 최대 리스트 크기가 5로 설정하였으므로 추가 불가
list.insert(item: 6)
// 현재 리스트 카운트 : 5
print(list.count())
for i in 0..<list.count() {
print(list.get(index: i))
// 1
// 2
// 3
// 4
// 5
}
Implementation
// 4번째 요소 삭제
list.remove(at: 3)
// 현재 리스트 카운트 : 4
print(list.count())
for i in 0..<list.count() {
print(list.get(index: i))
// 1
// 2
// 3
// 5
}
list.removeAll()
// 현재 리스트 카운트 : 0
print(list.count())
References
[1] [Swift 자료구조 ch01] 자료구조 살펴보기 : https://
kor45cw.tistory.com/238
[2] Swift로 자료구조, 알고리즘 공부하기 (3) - Array List : https://
kor45cw.tistory.com/3
[3] Swift의 Array 완전 정복 – 01. 생성과 조작 : https://
soooprmx.com/archives/7045
[4] (Swift 기초) 자료구조(Collection) - 배열(Array) (2) : https://
m.blog.naver.com/PostView.nhn?
blogId=el_vin&logNo=221019918085&categoryNo=10&proxy
Referer=&proxyReferer=https:%2F%2Fwww.google.com%2F
[5] [Swift4] 컬렉션 타입 ::: Array / Dictionary / 메소드 /
Optional / nil : https://yereol.tistory.com/9
References
[6] Swift Language 배열 : https://sodocumentation.net/ko/
swift/topic/284/배열
[7] Swift Collection Types : https://
www.hohyeonmoon.com/blog/swift-collection-types/
[8] 10장 Swift 기초 8, 묶음(collection) 자료 구조, 배열(array) :
https://www.youtube.com/watch?v=UgMHDoFpN9M
[9] Swift의 Sequence와 Collection에 대해 알아야 하는것들 :
https://academy.realm.io/kr/posts/try-swift-soroush-
khanlou-sequence-collection/
[10] 자료구조: Linked List 대 ArrayList : http://
www.nextree.co.kr/p6506/
Thank you!
Ad

Recommended

[Commit Again] 1주차 STL study
[Commit Again] 1주차 STL study
경 송
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
 
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
Bill Kim
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
 
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
Bill Kim
 
Android Programming - AdapterView
Android Programming - AdapterView
Jake Yoon
 
[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
 
[Swift] Data Structure - Linked List
[Swift] Data Structure - Linked List
Bill Kim
 
제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기
sang doc Lee
 
Python 강좌 발표 자료
Python 강좌 발표 자료
Soobin Jung
 
3.자료구조
3.자료구조
명준 김
 
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
Jiho Lee
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
 
파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기
Yong Joon Moon
 
Data Structures
Data Structures
skku_npc
 
연결 리스트(기초)
연결 리스트(기초)
Lee Geonhee
 
[Swift] Tuple
[Swift] Tuple
Bill Kim
 
Binary Search
Binary Search
skku_npc
 
Python + Excel
Python + Excel
POSTECH
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
 
Python 스터디
Python 스터디
sanghyuck Na
 
05_STL컨테이너정리
05_STL컨테이너정리
noerror
 
자료구조6보고서
자료구조6보고서
KimChangHoen
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
 
Project#6 오탈자 검사 D0 Hwp
Project#6 오탈자 검사 D0 Hwp
Kimjeongmoo
 
amugona study 3rd
amugona study 3rd
who7117
 
20 swift 집합형
20 swift 집합형
Changwon National University
 
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
 
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
ultrasuperrok
 

More Related Content

What's hot (20)

제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기
sang doc Lee
 
Python 강좌 발표 자료
Python 강좌 발표 자료
Soobin Jung
 
3.자료구조
3.자료구조
명준 김
 
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
Jiho Lee
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
 
파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기
Yong Joon Moon
 
Data Structures
Data Structures
skku_npc
 
연결 리스트(기초)
연결 리스트(기초)
Lee Geonhee
 
[Swift] Tuple
[Swift] Tuple
Bill Kim
 
Binary Search
Binary Search
skku_npc
 
Python + Excel
Python + Excel
POSTECH
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
 
Python 스터디
Python 스터디
sanghyuck Na
 
05_STL컨테이너정리
05_STL컨테이너정리
noerror
 
자료구조6보고서
자료구조6보고서
KimChangHoen
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
 
Project#6 오탈자 검사 D0 Hwp
Project#6 오탈자 검사 D0 Hwp
Kimjeongmoo
 
amugona study 3rd
amugona study 3rd
who7117
 
20 swift 집합형
20 swift 집합형
Changwon National University
 
제4장 sql 함수를 사용해보기
제4장 sql 함수를 사용해보기
sang doc Lee
 
Python 강좌 발표 자료
Python 강좌 발표 자료
Soobin Jung
 
3.자료구조
3.자료구조
명준 김
 
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
20111025 Excel의 VBA, 매크로. 그리고 파이썬으로 함께하는 반복작업
Jiho Lee
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
 
파이썬 엑셀_csv 처리하기
파이썬 엑셀_csv 처리하기
Yong Joon Moon
 
Data Structures
Data Structures
skku_npc
 
연결 리스트(기초)
연결 리스트(기초)
Lee Geonhee
 
[Swift] Tuple
[Swift] Tuple
Bill Kim
 
Binary Search
Binary Search
skku_npc
 
Python + Excel
Python + Excel
POSTECH
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
 
05_STL컨테이너정리
05_STL컨테이너정리
noerror
 
자료구조6보고서
자료구조6보고서
KimChangHoen
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
 
Project#6 오탈자 검사 D0 Hwp
Project#6 오탈자 검사 D0 Hwp
Kimjeongmoo
 
amugona study 3rd
amugona study 3rd
who7117
 

Similar to [Swift] Data Structure - Array (20)

[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
 
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
ultrasuperrok
 
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
Jaewook Byun
 
Javascript - Array
Javascript - Array
wonmin lee
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
S.O.P.T - Shout Our Passion Together
 
[Swift] Data Structure Introduction
[Swift] Data Structure Introduction
Bill Kim
 
Haskell study 2
Haskell study 2
Nam Hyeonuk
 
Python array.array 모듈 이해하기
Python array.array 모듈 이해하기
Yong Joon Moon
 
Swift2
Swift2
HyungKuIm
 
파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기
Yong Joon Moon
 
3 swift 컬렉션,흐름제어
3 swift 컬렉션,흐름제어
Changwon National University
 
자료구조 02 최종 보고서
자료구조 02 최종 보고서
pkok15
 
Javascript
Javascript
Hong Hyo Sang
 
알고리즘과 자료구조
알고리즘과 자료구조
영기 김
 
자료구조 트리 보고서
자료구조 트리 보고서
mil23
 
Project#2말의여행 Hwp
Project#2말의여행 Hwp
Kimjeongmoo
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf
kd19h
 
2012 Ds B2 02
2012 Ds B2 02
chl132435
 
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
 
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
코딩테스트 합격자 되기 2주차 스터디 - 리스트_딕셔너리
ultrasuperrok
 
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
데이터 분석 4 - 나만의 배열 기반 LIST, MyArrayList를 만들어보자
Jaewook Byun
 
Javascript - Array
Javascript - Array
wonmin lee
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
[SOPT] 데이터 구조 및 알고리즘 스터디 - #01 : 개요, 점근적 복잡도, 배열, 연결리스트
S.O.P.T - Shout Our Passion Together
 
[Swift] Data Structure Introduction
[Swift] Data Structure Introduction
Bill Kim
 
Python array.array 모듈 이해하기
Python array.array 모듈 이해하기
Yong Joon Moon
 
파이썬 Collections 모듈 이해하기
파이썬 Collections 모듈 이해하기
Yong Joon Moon
 
3 swift 컬렉션,흐름제어
3 swift 컬렉션,흐름제어
Changwon National University
 
자료구조 02 최종 보고서
자료구조 02 최종 보고서
pkok15
 
알고리즘과 자료구조
알고리즘과 자료구조
영기 김
 
자료구조 트리 보고서
자료구조 트리 보고서
mil23
 
Project#2말의여행 Hwp
Project#2말의여행 Hwp
Kimjeongmoo
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf
kd19h
 
2012 Ds B2 02
2012 Ds B2 02
chl132435
 
Ad

More from Bill Kim (19)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim
 
Design Pattern Introduction
Design Pattern Introduction
Bill Kim
 
[Swift] Visitor
[Swift] Visitor
Bill Kim
 
[Swift] Template Method
[Swift] Template Method
Bill Kim
 
[Swift] State
[Swift] State
Bill Kim
 
[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
 
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim
 
Design Pattern Introduction
Design Pattern Introduction
Bill Kim
 
[Swift] Visitor
[Swift] Visitor
Bill Kim
 
[Swift] Template Method
[Swift] Template Method
Bill Kim
 
[Swift] State
[Swift] State
Bill Kim
 
Ad

[Swift] Data Structure - Array

  • 3. Array List 자료구조에서 가장 기본이 되는 구조는 배열(리스트)입니다. 리스트는 크게 아래와 같이 2개의 리스트로 구분할 수 있습니다. 순차 리스트 : 배열을 기반으로 구현된 리스트 연결 리스트 : 메모리의 동적 할당을 기반으로 구현된 리스트 본 강의에서는 Swift 코드를 활용하여 순차 리스트에 대해서 살펴 봅니다.
  • 4. Concept 순차리스트는 아래와 같이 특정 자료를 정해진 공간안에서 순차적 으로 저장 및 가져오는 자료구조입니다.
  • 5. Features 순차 리스트(Array)의 특징을 살펴보면 아래와 같습니다. - 데이터를 순차적으로 저장합니다. - 데이터 중복 저장을 허용합니다. - 배열의 총 길이는 초기에 결정되어야 합니다.(변경 불가) - 인덱스 값을 기준으로 데이터의 참조가 가능합니다. - 삭제의 과정에서 데이터의 이동이 빈번하게 일어납니다.
  • 6. Implementation Swift를 활용하여 Array 를 구현해보겠습니다. 우선 필요한 메소드는 아래와 같습니다. - init() : 배열의 크기를 설정하는 초기화 함수 - insert() : 데이터 입력 - remove() : 특정 인덱스의 데이터 삭제 - removeAll() : 모든 데이터 삭제 - get() : 특정 인덱스의 데이터 값을 반환 - count() : 현재 배열의 크기를 반환
  • 7. Implementation 가장 데이터의 기본이 되는 Node 클래스 입니다. 해당 Node 클래스는 모든 데이터 형식을 받을 수 있도록 Generic 형태로 구현이 되어 있습니다. class Node<T> { fileprivate var data:T init(_ data: T) { self.data = data } }
  • 8. Implementation class ArrayList<T> { private var size:Int private var list:[Node<T>] init(size:Int) { self.list = [Node<T>]() self.size = size } func insert(item:T) { if self.list.count < size { self.list.append(Node<T>(item)) } } func remove(at:Int) { if self.list.count <= 0 { return } if self.list.count <= at { return } self.list.remove(at: at) } func removeAll() { self.list.removeAll() } func get(index:Int) -> T { return self.list[index].data } func count() -> Int { size = list.count return size } }
  • 9. Implementation 아래와 같이 사용이 가능합니다. let list:ArrayList<Int> = ArrayList(size: 5) list.insert(item: 1) list.insert(item: 2) list.insert(item: 3) list.insert(item: 4) list.insert(item: 5) // 최대 리스트 크기가 5로 설정하였으므로 추가 불가 list.insert(item: 6) // 현재 리스트 카운트 : 5 print(list.count()) for i in 0..<list.count() { print(list.get(index: i)) // 1 // 2 // 3 // 4 // 5 }
  • 10. Implementation // 4번째 요소 삭제 list.remove(at: 3) // 현재 리스트 카운트 : 4 print(list.count()) for i in 0..<list.count() { print(list.get(index: i)) // 1 // 2 // 3 // 5 } list.removeAll() // 현재 리스트 카운트 : 0 print(list.count())
  • 11. References [1] [Swift 자료구조 ch01] 자료구조 살펴보기 : https:// kor45cw.tistory.com/238 [2] Swift로 자료구조, 알고리즘 공부하기 (3) - Array List : https:// kor45cw.tistory.com/3 [3] Swift의 Array 완전 정복 – 01. 생성과 조작 : https:// soooprmx.com/archives/7045 [4] (Swift 기초) 자료구조(Collection) - 배열(Array) (2) : https:// m.blog.naver.com/PostView.nhn? blogId=el_vin&logNo=221019918085&categoryNo=10&proxy Referer=&proxyReferer=https:%2F%2Fwww.google.com%2F [5] [Swift4] 컬렉션 타입 ::: Array / Dictionary / 메소드 / Optional / nil : https://yereol.tistory.com/9
  • 12. References [6] Swift Language 배열 : https://sodocumentation.net/ko/ swift/topic/284/배열 [7] Swift Collection Types : https:// www.hohyeonmoon.com/blog/swift-collection-types/ [8] 10장 Swift 기초 8, 묶음(collection) 자료 구조, 배열(array) : https://www.youtube.com/watch?v=UgMHDoFpN9M [9] Swift의 Sequence와 Collection에 대해 알아야 하는것들 : https://academy.realm.io/kr/posts/try-swift-soroush- khanlou-sequence-collection/ [10] 자료구조: Linked List 대 ArrayList : http:// www.nextree.co.kr/p6506/