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

dropLast(_:)

Returns a subsequence containing all but the specified number of final elements. 指定された数の末尾要素以外すべてを含んでいる下位シーケンスを返します。

Declaration 宣言

func dropLast(_ k: Int = 1) -> Slice<LazyDropWhileCollection<Base>>
Available when Base conforms to Collection. BaseCollectionに準拠する時に利用可能です。

Parameters パラメータ

k

The number of elements to drop off the end of the collection. k must be greater than or equal to zero. コレクションの終わりで省く要素の数。kは、ゼロより大きいか等しくなければなりません。

Return Value 戻り値

A subsequence that leaves off the specified number of elements at the end. 指定された数の要素が末尾から無くなっているシーケンス。

Discussion 解説

If the number of elements to drop exceeds the number of elements in the collection, the result is an empty subsequence. 省かれることになる要素の数がコレクションの要素の数を越えるならば、結果は空の下位シーケンスです。


let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints "[1, 2, 3]"
print(numbers.dropLast(10))
// Prints "[]"

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the length of the collection. 計算量:O(1)、もしコレクションがRandomAccessCollectionに準拠するならば;そうでなければ、O(n)、そこでnはコレクションの長さです。