Instance MethodSwift

append(_:)

Adds a new element at the end of the array.

mutating func append(_ newElement: Element)

Parameters

newelement

The element to append to the array.

Overview

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

var numbers = [1, 2, 3, 4, 5]
numbers.append(100)
print(numbers)
// 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.