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

dropLast(_:)

Returns a sequence containing all but the given number of final elements. 与えられた数の末尾要素以外すべてを含んでいるあるシーケンスを返します。

Declaration 宣言

func dropLast(_ k: Int = 1) -> [Element]

Parameters パラメータ

n

The number of elements to drop off the end of the sequence. n must be greater than or equal to zero. シーケンスの末尾から取り除く要素の数。nは、ゼロより大きいか等しくなければなりません。

Return Value 戻り値

A sequence leaving off the specified number of elements. 指定された数の要素を取り除いたあるシーケンス。

Discussion 解説

The sequence must be finite. If the number of elements to drop exceeds the number of elements in the sequence, the result is an empty sequence. シーケンスは有限でなければなりません。取り除く要素の数がシーケンスの要素数を越えるならば、結果は空のシーケンスです。


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

Complexity: O(n), where n is the length of the sequence. 計算量:O(n)、ここでnはシーケンスの長さです。