我知道.append
有时会增加数组的容量并形成一个新的数组副本,但是.removeLast
是否会反过来减少数组的容量,并将其复制到一个更小的新数组中?
append
的复杂性承诺,它如下所述:removeLast()
的复杂性承诺:removeFirst
不应该减少数组的容量。补充一些细节,removeLast
也不会减少容量,而removeAll
默认情况下会减少容量,但它可以通过参数keepingCapacity
来改变这种行为。var arr = [Int]()
print(arr.capacity) // 0
arr.append(1)
print(arr.capacity) // 2
arr.append(2)
print(arr.capacity) // 2
arr.removeFirst()
print(arr.capacity) // 2
arr.removeLast()
print(arr.capacity) // 2 (though the array is now empty)
arr.removeAll(keepingCapacity: true)
print(arr.capacity) // 2
arr.removeAll()
print(arr.capacity) // 0