Generic Instance Method 総称体インスタンスメソッド

insert(contentsOf:at:)

Inserts the elements of a sequence into the collection at the specified position. あるシーケンスに属する複数の要素をコレクションへ指定された位置で挿入します。

Declaration 宣言

mutating func insert<C>(contentsOf newElements: C, at i: Int) where C : Collection, Element == C.Element

Parameters パラメータ

newElements

The new elements to insert into the collection. コレクションへ挿入される新しいいくらかの要素。

i

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

Discussion 解説

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

Here’s an example of inserting a range of integers into an array of the same type: ここに整数の範囲を同じ型の配列へ挿入する例があります:


var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints "[1, 2, 3, 100, 101, 102, 103, 4, 5]"

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

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

See Also 参照

Adding Elements 要素の追加