本文翻译自:Add an element to an array in Swift
Suppose I have an array, for example: 假设我有一个数组,例如:
var myArray = ["Steve", "Bill", "Linus", "Bret"]
And later I want to push/append an element to the end of said array, to get: 然后我想将一个元素推/追加到所述数组的末尾,以获得:
["Steve", "Bill", "Linus", "Bret", "Tim"]
What method should I use? 我应该使用什么方法?
And what about the case where I want to add an element to the front of the array? 那我想在数组的前面添加一个元素呢? Is there a constant time unshift? 是否有固定的时间不变?
#1楼
参考: