Instance Method インスタンスメソッド

insert(_:at:)

Inserts a new element into the collection at the specified position. ある新しい要素をコレクションへ指定された位置で挿入します。

Declaration 宣言

mutating func insert(_ newElement: UInt8, at i: Int)

Parameters パラメータ

newElement

The new element to insert into the collection. コレクションに挿入されることになる新しい要素。

i

The position at which to insert the new element. index must be a valid index into the collection. そこで新しい要素が挿入されることになる位置。indexはコレクションに対する有効なインデックスでなければなりません。

Discussion 議論

The new element is inserted before the element currently at the specified index. If you pass the collection’s endIndex property as the index parameter, the new element is appended to the collection. 新しい要素は、指定されたインデックスでの現在の要素の前に挿入されます。あなたがコレクションのendIndexプロパティをindexパラメータとして渡すならば、新しい要素がコレクションに追加されます。


var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)


print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"

Calling this method may invalidate any existing indices for use with this collection. このメソッドを呼び出すことは、このコレクションで使うためのあらゆる既存のインデックスを無効にします。

Complexity: O(n), where n is the length of the collection. If i == endIndex, this method is equivalent to append(_:). 計算量:O(n)、ここでnはコレクションの長さです。i == endIndexならば、このメソッドはappend(_:)に等しいです。