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

reversed()

Returns a view presenting the elements of the collection in reverse order. コレクションの要素を逆順に表しているある見方を返します。

Declaration 宣言

func reversed() -> ReversedCollection<Array<Element>>

Discussion 解説

You can reverse a collection without allocating new space for its elements by calling this reversed() method. A ReversedCollection instance wraps an underlying collection and provides access to its elements in reverse order. This example prints the characters of a string in reverse order: あなたは、あるコレクションを逆順にすることがそれの要素に対して新しい空間を割り当てることなくreversed()メソッドによって可能です。ReversedCollectionインスタンスは、基礎をなすコレクションをラップして、それの要素へのアクセスを逆順で提供します。この例は、ある文字列に属する文字を逆順で出力します。


let word = "Backwards"
for char in word.reversed() {
    print(char, terminator: "")
}
// Prints "sdrawkcaB"

If you need a reversed collection of the same type, you may be able to use the collection’s sequence-based or collection-based initializer. For example, to get the reversed version of a string, reverse its characters and initialize a new String instance from the result. あなたが同じ型での逆にされたコレクションを必要とするならば、あなたはコレクションのもつシーケンス基盤のまたはコレクション基盤のイニシャライザを使うことができるでしょう。例えば、ある文字列の逆版を得るには、それの文字を逆にして、新しいStringインスタンスをその結果から初期化してください。


let reversedWord = String(word.reversed())
print(reversedWord)
// Prints "sdrawkcaB"

Complexity: O(1) 計算量:O(1)

See Also 参照

Reordering an Array's Elements 配列の要素を再配列します