Accesses the element at the specified position.


subscript(index: Int) -> Element { get set }



The position of the element to access. index must be greater than or equal to startIndex and less than endIndex.


The following example uses indexed subscripting to update an array’s second element. After assigning the new value ("Butler") at a specific position, that value is immediately available at that same position.

var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
// Prints "Butler"

Complexity: Reading an element from an array is O(1). Writing is O(1) unless the array’s storage is shared with another array or uses a bridged NSArray instance as its storage, in which case writing is O(n), where n is the length of the array.

See Also

Accessing Elements

var first: Element?

The first element of the collection.

var last: Element?

The last element of the collection.

subscript(Range<Int>) -> ArraySlice<Element>

Accesses a contiguous subrange of the array’s elements.

subscript(Range<Int>) -> Slice<Array<Element>>

Accesses a contiguous subrange of the collection’s elements.

subscript<R>(R) -> ArraySlice<Element>

Accesses the contiguous subrange of the collection’s elements specified by a range expression.

func randomElement() -> Element?

Returns a random element of the collection.

func randomElement<T>(using: inout T) -> Element?

Returns a random element of the collection, using the given generator as a source for randomness.