Swift's initial initial array (ii)

Source: Internet
Author: User

In Apple's development documentation, the array also provides additional operational algorithms:

1. Sort function:

Sorts the array. Depending on the collation you specify, see the following code:

var array = [2, 3, 4, 5]array.sort{$0 < $1}println (array)

Print Now: [2, 3, 4, 5]
The fact that the sort braces are in the middle is a closure, which we'll learn later.
Suppose flashbacks are arranged, code such as the following:
array.sort{$0 > $1}println (Array)

Output: [5, 4, 3, 2]

2. Reverse function
Returned in accordance with the index flashback of the array, for example:
Let Newa = Array.reverse () println (Newa)

The output should now be: [2, 3, 4, 5]
You can see the array flashbacks.



3. Filter
Filter array values based on certain criteria: for example:

Let newb = array.filter{$0% 2 = = 0}println (newb)

The output is: [2, 4]

4. Map
Apply the rules in the closure to the current array and return a new array:
var NewArray = array.map{$0 * 3}PRINTLN (NewArray)

The rules in closures are multiplied by 3 for each number so our output is: [6, 9, 12, 15]

5. Reduce
The official explanation is that each element is manipulated through a closure and then a separate value is returned
Example:
Let addres = Array.reduce (2) {$ + $1}println (addres)

Reduce (2) 2 in parentheses represents the initial value. Operation within the closure

The return value we get is: 16

Ps:swift also provides a slice class, an official descriptive narrative such as the following:

The Slice class represents a one-dimensional subset of an array, specified by three Parameters:start offset, size, and St Ride. The start offset is the index of the first element of the array, which is part of the subset. The size is the total number of elements in the subset. Stride is the distance between each successive an array element to an include in the subset.

For example, with an array of size, and a slice with offset 1, size 3 and Stride 2, the subset consists of array elemen TS 1, 3, and 5.

Simply put, slice is a subclass of array, including three parts: Start offset. Size, stride.

Look at a sample:

var slice:slice = Array[1...3]array = Array (slice) slice = slice (array) println (slice)



You are welcome to learn from each other.





Swift's initial initial array (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.