【Swift初见】Swift数组(二)
在苹果的开发文档中对Array还提供了其它的操作算法:
1、Sort函数:
对数组进行排序。依据指定的排序规则,看以下的代码:
var array = [2, 3, 4, 5]
array.sort{$0 < $1}
println(array)
此时打印出:[2, 3, 4, 5]
事实上sort大括号中面是一个闭包,这个以后再学习。
假设倒叙排列,代码例如以下:
array.sort{$0 > $1}
println(array)
输出为:[5, 4, 3, 2]
2、reverse函数
依照数组的index倒叙排列返回,比方:
let newA = array.reverse()
println(newA)
此时的输出应该为:[2, 3, 4, 5]
能够看出数组倒叙排列了。
3、filter
依据某些条件来筛选数组值:比方:
let newB = array.filter{$0 % 2 == 0}
println(newB)
输出为:[2, 4]
4、map
对当前数组运用闭包内的规则然后返回一个新的数组:
var newArray = array.map{$0 * 3}
println(newArray)
闭包内的规则是每一个数字乘以3 所以我们的输出为:[6, 9, 12, 15]
5、reduce
官方解释为:通过闭包内对每一个元素进行操作然后返回一个单独的值
比方:
let addRes = array.reduce(2){$0 + $1}
println(addRes)
reduce(2)括号里的2表示初始值。闭包内为操作
我们得到的返回值为:16
PS:Swift还提供了一个Slice类,官方描写叙述例如以下:
The slice class represents a one-dimensional subset of an array, specified by three parameters: start offset, size, and stride. The start offset is the index of the first element of the array that is part of the subset. The size is the total number of elements
in the subset. Stride is the distance between each successive array element to include in the subset.
For example, with an array of size 10, and a slice with offset 1, size 3 and stride 2, the subset consists of array elements 1, 3, and 5.
简单的说就是Slice是Array的一个子类,包括三个部分:start offset。 size, stride。
看个样例:
var slice : Slice = array[1...3]
array = Array(slice)
slice = Slice(array)
println(slice)
欢迎大家相互学习。
【Swift初见】Swift数组(二)的更多相关文章
- Swift语法基础入门二(数组, 字典, 字符串)
Swift语法基础入门二(数组, 字典, 字符串) 数组(有序数据的集) *格式 : [] / Int / Array() let 不可变数组 var 可变数组 注意: 不需要改变集合的时候创建不可变 ...
- Swift 里 Array (二)初始化
init() 函数 在 Array 里 public init() { _buffer = _Buffer() } 以Buffer 是 _ContiguousArrayBuffer 为例. 即初始化了 ...
- openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 二
openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...
- hiho一下121周 后缀数组二·重复旋律2
后缀数组二·重复旋律2 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列.小Hi ...
- Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift
About Swift 关于Swift 官方文档的翻译,仅供参考,本人英语本就不好,边学边翻译,不喜勿喷. Swift is a new programming language for iOS, O ...
- Java基本语法-----java数组(一维数组二维数组)
嘿嘿!你们懂的,又是图片,委屈大家了. java数组(一维数组二维数组) [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个"顶"字,你就 ...
- C++学习(十五)(C语言部分)之 数组二
数组大纲 多维数组 二维数组 重点 (三位以上基本不会用到) 都是用来存数据 一个班有20个人 可以用一维数组存20个人的年龄 int age[20]; 一个年级10个班 每个班20人 int age ...
- Swift学习——Swift基础具体解释(一)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/34807025 注:由于基础部分在Swi ...
- javascript数据结构和算法 第二章 (数组) 二
字符串表示的数组 join() 和 toString() 函数返回数组的字符串表示.这两个函数通过将数组中的元素用逗号分隔符切割,返回字符串数组表示. 这里有个样例: var names = [&qu ...
- hihocoder #1407 : 后缀数组二·重复旋律2
#1407 : 后缀数组二·重复旋律2 Time Limit:5000ms Case Time Limit:1000ms Memory Limit:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢 ...
随机推荐
- java中多线程知识
参考:http://www.cnblogs.com/wxd0108/p/5479442. 引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只 ...
- 关于fetch
前言: fetch是用来取代传统的XMLHttpRequest的. 它的优点很多,包括链式调用的语法.返回promise等. 什么是fetch? fetch api是基于promise的设计,它是为了 ...
- Tuples as return values
Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is ...
- String slices
String slices A segment of a string is called a slice. Selecting a slice is similar selecting a char ...
- 超轻便的 Cache_Lite 文件缓存
Cache_Lite提供了快速,轻便和安全的缓存系统.它针对文件容器进行了优化,并且防止缓存损坏(因为它使用文件锁定和/或散列测试). 个人感觉还是挺方便的. Cache_Lite 官方参考地址. C ...
- Kubernetes1.5 集成Heapster
Kubernetes1.5 集成Heapster Heapster是kubernetes集群监控工具.在1.2的时候,kubernetes的监控需要在node节点上运行cAdvisor作为agent收 ...
- [Codeforces 816A]Karen and Morning
题目大意:给你一个时间(hh:mm),求最少经过多少分钟才能使这个时间变成回文. 解题思路:模拟,先判断0的情况,然后每过1分钟判断一次即可. C++ Code: #include<cstdio ...
- 监控web服务(http,本地 / 远程监控nginx)
监控 httpd 服务一: #!/bin/bash #描述: 秒级别监控 http 服务 while [ 1 -lt 2 ] do sleep 10 ai=`netstat -ntl | grep & ...
- HDU 4976 A simple greedy problem. 贪心+DP
题意: 给定n<=1000个小兵,A每次都能使小兵掉1点血,B每次能使所有小兵掉1点血,A.B轮流攻击,每次轮到A他会选择是否攻击,轮到B必须攻击.求A最多能杀死多少小兵.(当小兵血量为1时被攻 ...
- 以Append方式打开文件,设置偏移量无效
#include<stdio.h> int main() { FILE * fd = fopen("btoo1.c", "ab+"); fpos_t ...