[Swift] (자료구조) 배열 / Array
2024. 12. 27. 18:23ㆍ🐣/자료구조
1. 배열 (Array)
- 정의: 고정된 순서로 데이터를 저장하는 선형 자료구조.
- 특징:
- 순서가 중요한 데이터 저장에 적합.
- 상수 시간 복잡도(O(1))로 인덱스를 통해 접근 가능.
- 장점:
- 간단하고 사용이 쉬움.
- 순차적 데이터 처리에 적합.
- 단점:
- 크기 변경이 비효율적.
// An array of 'Int' elements
let oddNumbers = [1, 3, 5, 7, 9, 11, 13, 15]
// An array of 'String' elements
let streets = ["Albemarle", "Brandywine", "Chesapeake"]
// Shortened forms are preferred
var emptyDoubles: [Double] = []
// The full type name is also allowed
var emptyFloats: Array<Float> = Array()
var digitCounts = Array(repeating: 0, count: 10)
print(digitCounts)
// Prints "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
for street in streets {
print("I don't live on \(street).")
}
// Prints "I don't live on Albemarle."
// Prints "I don't live on Brandywine."
// Prints "I don't live on Chesapeake."
if oddNumbers.isEmpty {
print("I don't know any odd numbers.")
} else {
print("I know \(oddNumbers.count) odd numbers.")
}
// Prints "I know 8 odd numbers."
if let firstElement = oddNumbers.first, let lastElement = oddNumbers.last {
print(firstElement, lastElement, separator: ", ")
}
// Prints "1, 15"
print(emptyDoubles.first, emptyDoubles.last, separator: ", ")
// Prints "nil, nil"
print(oddNumbers[0], oddNumbers[3], separator: ", ")
// Prints "1, 7"
print(emptyDoubles[0])
// Triggers runtime error: Index out of range
var students = ["Ben", "Ivy", "Jordell"]
students.append("Maxime")
students.append(contentsOf: ["Shakia", "William"])
// ["Ben", "Ivy", "Jordell", "Maxime", "Shakia", "William"]
students.insert("Liam", at: 3)
// ["Ben", "Ivy", "Jordell", "Liam", "Maxime", "Shakia", "William"]
// Ben's family is moving to another state
students.remove(at: 0)
// ["Ivy", "Jordell", "Liam", "Maxime", "Shakia", "William"]
// William is signing up for a different class
students.removeLast()
// ["Ivy", "Jordell", "Liam", "Maxime", "Shakia"]
if let i = students.firstIndex(of: "Maxime") {
students[i] = "Max"
}
// ["Ivy", "Jordell", "Liam", "Max", "Shakia"]
var numbers = [1, 2, 3, 4, 5]
var numbersCopy = numbers
numbers[0] = 100
print(numbers)
// Prints "[100, 2, 3, 4, 5]"
print(numbersCopy)
// Prints "[1, 2, 3, 4, 5]"
// An integer type with reference semantics
class IntegerReference {
var value = 10
}
var firstIntegers = [IntegerReference(), IntegerReference()]
var secondIntegers = firstIntegers
// Modifications to an instance are visible from either array
firstIntegers[0].value = 100
print(secondIntegers[0].value)
// Prints "100"
// Replacements, additions, and removals are still visible
// only in the modified array
firstIntegers[0] = IntegerReference()
print(firstIntegers[0].value)
// Prints "10"
print(secondIntegers[0].value)
// Prints "100"
var numbers = [1, 2, 3, 4, 5]
var firstCopy = numbers
var secondCopy = numbers
// The storage for 'numbers' is copied here
numbers[0] = 100
numbers[1] = 200
numbers[2] = 300
// 'numbers' is [100, 200, 300, 4, 5]
// 'firstCopy' and 'secondCopy' are [1, 2, 3, 4, 5]
let colors = ["periwinkle", "rose", "moss"]
let moreColors: [String?] = ["ochre", "pine"]
let url = URL(fileURLWithPath: "names.plist")
(colors as NSArray).write(to: url, atomically: true)
// true
(moreColors as NSArray).write(to: url, atomically: true)
// error: cannot convert value of type '[String?]' to type 'NSArray'
'🐣 > 자료구조' 카테고리의 다른 글
[Swift] (자료구조) 해쉬 / Hash (0) | 2024.03.06 |
---|---|
[Swift] (자료구조) 이진 탐색 트리 / Binary Search Tree (0) | 2022.09.01 |
[Swift] (자료구조) 트리와 이진 트리 / Binary Tree (0) | 2022.09.01 |
[Swift] (자료구조) 우선순위 큐 / Priority Queue (0) | 2022.08.26 |
[Swift] (자료구조) 큐 / Queue (0) | 2022.08.06 |