Swift Array常用 Api

优质
小牛编辑
127浏览
2023-12-01

Swift 5.x Array常用api

1. 数组的添加操作


  • 1.1 在末尾添加一个元素
var numbers = [Int](2...7)
print(numbers)
numbers.append(100)
print(numbers)

输出结果:

[2, 3, 4, 5, 6, 7]
[2, 3, 4, 5, 6, 7, 100]

  • 1.2 在末尾添加多个元素
var numbers = [Int](2...7)
print(numbers)
numbers.append(contentsOf: 20...22)
print(numbers)

输出结果:

[2, 3, 4, 5, 6, 7]
[2, 3, 4, 5, 6, 7, 20, 21, 22]

  • 1.3 在指定位置插入一个元素
var numbers = [Int](2...7)
print(numbers)
numbers.insert(100, at: 2)
print(numbers)

输出结果:

[2, 3, 4, 5, 6, 7]
[2, 3, 100, 4, 5, 6, 7]

  • 1.4 在指定位置插入多个元素
var numbers = [Int](2...7)
print(numbers)
numbers.insert(contentsOf: 10...15, at: 1)
print(numbers)

输出结果:

[2, 3, 4, 5, 6, 7]
[2, 10, 11, 12, 13, 14, 15, 3, 4, 5, 6, 7]

字符串作为序列插入(字符串也是Collection, ElementCharacter类型)

var chars: [Character] = ["a", "b", "c"]
chars.insert(contentsOf: "hello", at: 0)
print(chars)

输出结果:

["h", "e", "l", "l", "o", "a", "b", "c"]

2. 数组的移除操作


  • 2.1移除指定索引上的单个元素
var chars: [Character] = ["a", "b", "c"]
let removedChar = chars.remove(at: 1)
print(removedChar)
print(chars)

输出结果:

b
["a", "c"]

  • 2.2 移除首元素
var chars: [Character] = ["a", "b", "c"]
let removedChar = chars.removeFirst()
print(removedChar)
print(chars)

输出结果:

a
["b", "c"]

注: 如果数组为空, 调用移除会报错

Fatal error: Can't remove first element from an empty collection...

  • 2.3 移除末尾元素
var chars: [Character] = ["a", "b", "c"]
let removedChar = chars.removeLast()
print(removedChar)
print(chars)

输出结果:

c
["a", "b"]

注: 如果数组为空, 调用移除会报错

Fatal error: Can't remove first element from an empty collection...

  • 2.4 安全的移除最后一个元素
var chars: [Character] = ["a", "b", "c"]
let removedChar = chars.popLast()
print(removedChar)
print(chars)

输出结果:

Optional("c")
["a", "b"]

  • 2.5 移除数组前面多个元素
var chars: [Character] = ["a", "b", "c"]
chars.removeFirst(2)
print(chars)

输出结果:

["c"]

注: 如果移除个数超过数组长度会报错

Can't remove more items from a collection than it contains:...

  • 2.6 移除数组后面多个元素
var chars: [Character] = ["a", "b", "c"]
chars.removeLast(2)
print(chars)

输出结果:

["a"]

注: 如果移除个数超过数组长度会报错

Can't remove more items from a collection than it contains:...

  • 2.7 移除全部元素
var chars: [Character] = ["a", "b", "c"]
chars.removeAll()
print(chars)

输出结果:

[]

  • 2.8 移除全部元素, 保留数组容量
var chars: [Character] = ["a", "b", "c", "d", "e"]
chars.removeAll(keepingCapacity: true)
print(chars.capacity)

输出结果:

5

  • 2.9 移除给定范围的元素
var chars: [Character] = ["a", "b", "c", "d", "e"]
chars.removeSubrange(1...3)
print(chars)

输出结果:

["a", "e"]

注: 如果超出范围, 同样会报错

Fatal error: Array replace: subrange extends past the end: file ...

3. 通过drop得到一个数组切片


  • 3.1 移除原数组前面指定个数的元素得到一个ArraySlice
let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice = array.dropFirst()
print(arraySlice)
print(array)

输出结果:

[2, 10, 1, 0, 100, 55, 99]
[5, 2, 10, 1, 0, 100, 55, 99]

  • 3.2 移除原数组后面指定个数元素得到一个ArraySlice
let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice = array.dropLast()
print(arraySlice)
print(array)

输出结果:

[5, 2, 10, 1, 0, 100, 55]
[5, 2, 10, 1, 0, 100, 55, 99]

  • 3.3 移除原数组符合指定条件的元素得到一个ArraySlice
let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice1 = array.dropFirst(4)
let arraySlice2 = array.dropLast(4)
let arraySlice3 = array.drop(while: {$0 < 15})
print(arraySlice1)
print(arraySlice2)
print(arraySlice3)
print(array)

输出结果:

[0, 100, 55, 99]
[5, 2, 10, 1]
[100, 55, 99]
[5, 2, 10, 1, 0, 100, 55, 99]

4. 通过prefix得到一个数组切片


let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice1 = array.prefix(upTo: 4)
let arraySlice2 = array.prefix(4)
let arraySlice3 = array.prefix(through: 4)
let arraySlice4 = array.prefix(while: {$0 < 10})
print(arraySlice1)
print(arraySlice2)
print(arraySlice3)
print(arraySlice4)

输出结果:

[5, 2, 10, 1]
[5, 2, 10, 1]
[5, 2, 10, 1, 0]
[5, 2]

5. 通过suffix得到一个数组切片


let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice1 = array.suffix(3)
let arraySlice2 = array.suffix(from: 2)
print(arraySlice1)
print(arraySlice2)

输出结果:

[100, 55, 99]
[10, 1, 0, 100, 55, 99]

6. 通过Range得到一个数组切片


let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice1 = array[1...3]
let arraySlice2 = array[...2]
let arraySlice3 = array[2...]
let arraySlice4 = array[2..<4]
let arraySlice5 = array[...]
print(arraySlice1)
print(arraySlice2)
print(arraySlice3)
print(arraySlice4)
print(arraySlice5)

输出结果:

[2, 10, 1]
[5, 2, 10]
[10, 1, 0, 100, 55, 99]
[10, 1]
[5, 2, 10, 1, 0, 100, 55, 99]

7. 将ArraySlice转成Array


let array = [5, 2, 10, 1, 0, 100, 55, 99]
let arraySlice = array[1...3]
let newArray = Array(arraySlice)
print(newArray)

输出结果:

[2, 10, 1]

注: ArraySlice与原Array共享内存, 但操作Array或ArraySlice并不会影响到对方, 对其一执行操作会进行copy操作

8. 数组元素随机化shuffle


var array1 = [Int](1...8)
array1.shuffle()
print(array1)

let array2 = [Int](1...8)
let shuffledArr = array2.shuffled()
print(shuffledArr)

输出结果:

[5, 6, 8, 1, 3, 2, 4, 7]
[4, 3, 2, 8, 7, 5, 6, 1]

9. 数组的逆序 reverse


var array1 = [Int](1...8)
array1.reverse()
print(array1)

let array2 = [Int](1...8)
let reversedArr = array2.reversed()
print(reversedArr)

输出结果:

[8, 7, 6, 5, 4, 3, 2, 1]
ReversedCollection<Array<Int>>(_base: [1, 2, 3, 4, 5, 6, 7, 8])

注: reversedArr 不是实际上的Array类型

10. 数组的分组 partition


var array = [10, 20, 45, 30, 98, 101, 30, 115, 30, 4]
let index = array.partition { (element) -> Bool in
    element > 30
}
print(index)
let partition1 = array[..<index]
let partition2 = array[index...]
print(partition1)
print(partition2)
print(array)

输出结果:

6
[10, 20, 4, 30, 30, 30]
[101, 115, 98, 45]
[10, 20, 4, 30, 30, 30, 101, 115, 98, 45]

注: 将数组以某个条件分组, 数组前半部分都是不符合条件的元素, 数组后半部分都是符合条件的元素 注: index 是轴点索引(界限索引) 注: 该方法是不稳定的, 顺序会发生改变

11. 数组的排序


  • 11.1 在原数组上将元素排序, 只能作用与数组变量
var array = [10, 20, 45, 30, 98, 101, 30, 115, 30, 4]
array.sort()
print(array)

输出结果:

[4, 10, 20, 30, 30, 30, 45, 98, 101, 115]

  • 11.2 返回原数组的排序结果数组, 可以作用在数组变量和常量上
let array = [10, 20, 45, 30, 98, 101, 30, 115, 30, 4]
let sortedArray = array.sorted()
print(sortedArray)

输出结果:

[4, 10, 20, 30, 30, 30, 45, 98, 101, 115]

12. 交换数组的两个元素


var array = [10, 20, 45, 30, 4]
print(array)
array.swapAt(array.startIndex, array.endIndex - 1)
print(array)

输出结果:

[10, 20, 45, 30, 4]
[4, 20, 45, 30, 10]

13. 字符串数组的拼接


var array = ["hello", "world"]
let string = array.joined()
let stringWithSeparator = array.joined(separator: " ")
print(string)
print(stringWithSeparator)

输出结果:

helloworld
hello world

14. 元素尾Sequence数组的拼接


  • 14.1 joined() 拼接数组里的所有元素尾一个更大的Sequence
let ranges = [0..<3, 8..<10, 15..<17]
for range in ranges {
    print(range)
}

for i in ranges.joined() {
    print(i)
}

输出结果:

0..<3
8..<10
15..<17
0
1
2
8
9
15
16

  • 14.2 joined(separator) 以给定的分隔符拼接数组里的所有元素尾一个更大的Sequence
let nestedNumbers = [[Int](0..<3), [Int](8..<10), [Int](15..<17)]
for range in nestedNumbers {
    print(range)
}
let joined = nestedNumbers.joined(separator: [-1, -2])
print(Array(joined))

输出结果:

[0, 1, 2]
[8, 9]
[15, 16]
[0, 1, 2, -1, -2, 8, 9, -1, -2, 15, 16]