Return Value 戻り値
A subsequence starting at the beginning of this collection with at most max
elements.
このコレクションの先端で始まる多くともmax
要素の下位シーケンス。
Availability 有効性
Technology
func prefix(_ maxLength: Int
) -> Self.SubSequence
A subsequence starting at the beginning of this collection with at most max
elements.
このコレクションの先端で始まる多くともmax
要素の下位シーケンス。
maxLength
The maximum number of elements to return. max
must be greater than or equal to zero.
返される要素の最大限の数。max
は、ゼロより大きいか等しくなければなりません。
If the maximum length exceeds the number of elements in the collection, the result contains all the elements in the collection. 最大長がコレクションの要素の数を超えるならば、結果はコレクションの要素すべてを含みます。
let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"
Complexity
O(1) if the collection conforms to Random
; otherwise, O(k), where k is the number of elements to select from the beginning of the collection.