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

insert(_:at:)

Inserts a new element at the specified position. ある新しい要素を指定された位置で挿入します。

Declaration 宣言

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

Parameters パラメータ

newElement

The new element to insert into the array. 配列に挿入されることになる新しい要素。

i

The position at which to insert the new element. index must be a valid index of the array or equal to its endIndex property. そこで新しい要素が挿入されることになる位置。indexは、配列の有効なインデックスまたはそれのendIndexプロパティでなければなりません。

Discussion 解説

The new element is inserted before the element currently at the specified index. If you pass the array’s endIndex property as the index parameter, the new element is appended to the array. 新しい要素は、指定されたインデックスでの現在の要素の前に挿入されます。あなたが配列の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]"

Complexity: O(n), where n is the length of the array. If i == endIndex, this method is equivalent to append(_:). 計算量:O(n)、ここでnは配列の長さです。i == endIndexならば、このメソッドはappend(_:)に相当します。

Relationships 関係

From Protocol 由来プロトコル

See Also 参照

Adding Elements 要素の追加