Instance Method


Adds a new element at the end of the array.


mutating func append(_ newElement: Element)



The element to append to the array.


Use this method to append a single element to the end of a mutable array.

var numbers = [1, 2, 3, 4, 5]
// Prints "[1, 2, 3, 4, 5, 100]"

Because arrays increase their allocated capacity using an exponential strategy, appending a single element to an array is an O(1) operation when averaged over many calls to the append(_:) method. When an array has additional capacity and is not sharing its storage with another instance, appending an element is O(1). When an array needs to reallocate storage before appending or its storage is shared with another copy, appending is O(n), where n is the length of the array.

Complexity: O(1) on average, over many calls to append(_:) on the same array.


See Also

Adding Elements

func insert(Element, at: Int)

Inserts a new element at the specified position.

func insert<C>(contentsOf: C, at: Int)

Inserts the elements of a sequence into the collection at the specified position.

func replaceSubrange<C>(Range<Int>, with: C)

Replaces a range of elements with the elements in the specified collection.

func replaceSubrange<C, R>(R, with: C)

Replaces the specified subrange of elements with the given collection.

func reserveCapacity(Int)

Reserves enough space to store the specified number of elements.