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

replaceSubrange(_:with:)

Replaces the specified subrange of elements with the given collection. いくらかの要素からなる指定された下位範囲を与えられたコレクションで置き換えます。

Declaration 宣言

mutating func replaceSubrange<C, R>(_ subrange: R, with newElements: C) where C : Collection, R : RangeExpression, Element == C.Element, Int == R.Bound

Parameters パラメータ

subrange

The subrange of the collection to replace. The bounds of the range must be valid indices of the collection. 置き換えることになるコレクションの下位範囲。この範囲の境界は、コレクションの有効なインデックスでなければなりません。

newElements

The new elements to add to the collection. コレクションに加える新しいいくらかの要素。

Discussion 解説

This method has the effect of removing the specified range of elements from the collection and inserting the new elements at the same location. The number of new elements need not match the number of elements being removed. このメソッドは、指定された範囲の要素をコレクションから取り除き、そして新しい要素を同じ場所に挿入する効果を持ちます。新しい要素の数は、削除される要素の数と一致する必要はありません。

In this example, three elements in the middle of an array of integers are replaced by the five elements of a Repeated<Int> instance. この例では、整数からなる配列の真ん中の3つの要素が、Repeated<Int>インスタンスの5つの要素によって置き換えられます。


 var nums = [10, 20, 30, 40, 50]
 nums.replaceSubrange(1...3, with: repeatElement(1, count: 5))
 print(nums)
 // Prints "[10, 1, 1, 1, 1, 1, 50]"

If you pass a zero-length range as the subrange parameter, this method inserts the elements of newElements at subrange.startIndex. Calling the insert(contentsOf:at:) method instead is preferred. あなたがゼロ長範囲をsubrangeパラメータとして渡すならば、このメソッドはnewElementsの要素をsubrange.startIndexで挿入します。insert(contentsOf:at:)メソッドを代わりに呼ぶことが好まれます。

Likewise, if you pass a zero-length collection as the newElements parameter, this method removes the elements in the given subrange without replacement. Calling the removeSubrange(_:) method instead is preferred. 同様に、あなたがゼロ長コレクションをnewElementsパラメータとして渡すならば、このメソッドは与えられた下位範囲の中の要素を置き換えることなく削除します。removeSubrange(_:)メソッドを代わりに呼ぶことが好まれます。

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 the call to this method simply appends the contents of newElements to the collection, the complexity is O(m). 計算量:O(n + m)、ここでnは配列の長さです、そしてmnewElementsの長さです。このメソッドへの呼び出しが単にnewElementsの内容をコレクションに追加するだけならば、計算量はO(m)です。

See Also 参照

Adding Elements 要素の追加