文档章节

2.使用集合

阿元
 阿元
发布于 2017/02/08 17:45
字数 1105
阅读 0
收藏 0

2.1数组

1array

输入图片说明

添加相同类型元素: 1.先把常量的值赋给变量

9> var VarFruitJay = FruitJay
VarFruitJay: [String] = 4 values {
[0] = "apple"
[1] = "pear"
[2] = "orange"
[3] = "banana"
}

2.再通过"append()"或"+="添加元素 //方法1

10> VarFruitJay.append("pineApple")
//方法2
11> VarFruitJay += ["peanut"]
12> VarFruitJay
$R2: [String] = 6 values {
[0] = "apple"
[1] = "pear"
[2] = "orange"
[3] = "banana"
[4] = "pineApple"
[5] = "peanut"
}

数组添加不同类型元素: 转换表示数组的类型为Any

17> var array1:[Any] = ["dog",1]
array1: [Any] = 2 values {
[0] = "dog"
[1] = 1
}
18> array1.append(1)
19> array1
$R3: [Any] = 3 values {
[0] = "dog"
[1] = 1
[2] = 1
}

替换

23> array1
R5:[Any]=3values[0]="dog"[1]=1[2]=124>array1[0]="Cat"25>array1R6: [Any] = 3 values {
[0] = "Cat"
[1] = 1
[2] = 1
}

删除值

26> array1.remove(at:2)
R7:Int=127>array1R8: [Any] = 2 values {
[0] = "Cat"
[1] = 1
}

删除最后一个元素

28> array1.removeLast()
R9:Int=129>array1R10: [Any] = 1 value {
[0] = "Cat"
}

删除全部元素

30> array1.removeAll()
31> array1
$R11: [Any] = 0 values

将值插入指定位置

33> var FruitJay : [Any] = ["apple",2017,true,"orange"]
FruitJay: [Any] = 4 values {
[0] = "apple"
[1] = 2017
[2] = true
[3] = "orange"
}
34> FruitJay.insert("Zoujie",at:3)
35> FruitJay
$R13: [Any] = 5 values {
[0] = "apple"
[1] = 2017
[2] = true
[3] = "Zoujie"
[4] = "orange"
}

数组合并 任意类型值数组合并相同类型值数组

36> var antherFood : [Any] = [2018,false,"zhouerke"]
antherFood: [Any] = 3 values {
[0] = "2018"
[1] = false
[2] = "zhouerke"
}
37> var Drink = ["cola","juice","water"]
Drink: [String] = 3 values {
[0] = "cola"
[1] = "juice"
[2] = "water"
}
38> var supper:[Any] = antherFood + Drink
40> supper
$R14: [Any] = 6 values {
[0] = 2018
[1] = false
[2] = "zhouerke"
[3] = "cola"
[4] = "juice"
[5] = "water"
}

数组添加数组

43> var Drink2 = ["orange juice","baisi cole"]
Drink2: [String] = 2 values {
[0] = "orange juice"
[1] = "baisi cole"
}
44> Drink2 += Drink
45> print (Drink2)
["orange juice", "baisi cole", "cola", "juice", "water"]

2.2字典

var PersonValue = ["face":20_000,"leg":1_000,"health":30_000_000,"hand":10_000,"head":3_000_000]
PersonValue: [String : Int] = 5 key/value pairs {
[0] = {
key = "health"
value = 30000000
}
[1] = {
key = "hand"
value = 10000
}
[2] = {
key = "leg"
value = 1000
}
[3] = {
key = "face"
value = 20000
}
[4] = {
key = "head"
value = 3000000
}
}

value可以声明Any任意类型元素

4> var Anydic : [String: Any] = ["1":1,"2":"2.1","3":3]
Anydic: [String : Any] = 3 key/value pairs {
[0] = {
key = "2"
value = "2.1"
}
[1] = {
key = "1"
value = 1
}
[2] = {
key = "3"
value = 3
}
}

Swift通过排列键来确保检索和访问的效率,你不能根据声明顺序确定存储顺序

查找条目

6> PersonValue ["face"]
$R0: Int? = 20000

7> var newDic = ["someKey":nil]
error: repl.swift:7:14: error: type of expression is ambiguous without more context
var newDic = ["someKey":nil]

8> var newDic = [nil:1]
error: repl.swift:8:14: error: type of expression is ambiguous without more context
var newDic = [nil:1]

字典键和值都不能指定为nil

8> PersonValue ["none"]
$R1: Int? = nil

字典将值返回为可选类型,是因为查询字典时使用的键可能不存在

添加&&更新条目

10> PersonValue ["none"] = 500_000
11> PersonValue ["face"] = 5
12> PersonValue
$R2: [String : Int] = 6 key/value pairs {
[0] = {
key = "none"
value = 500000
}
[1] = {
key = "health"
value = 30000000
}
[2] = {
key = "hand"
value = 10000
}
[3] = {
key = "leg"
value = 1000
}
[4] = {
key = "face"
value = 5
}
[5] = {
key = "head"
value = 3000000
}
}

删除条目

14> PersonValue ["face"] = nil//直接设置键的值为nil
15> PersonValue.removeValue(forKey:"leg")//语法删除
R4:Int?=100016>PersonValueR5: [String : Int] = 4 key/value pairs {
[0] = {
key = "none"
value = 500000
}
[1] = {
key = "health"
value = 30000000
}
[2] = {
key = "hand"
value = 10000
}
[3] = {
key = "head"
value = 3000000
}
}

数组嵌套

23> var array1:[Any] = ["name",23,"age"]
array1: [Any] = 3 values {
[0] = "name"
[1] = 23
[2] = "age"
}
24> var array2:[Any] = [true,"sex"]
array2: [Any] = 2 values {
[0] = true
[1] = "sex"
}
25> let array3 = ["weight","height"]
array3: [String] = 2 values {
[0] = "weight"
[1] = "height"
}
26> let sumArray = [array1,array2,array3]
sumArray: [[Any]] = 3 values {
[0] = 3 values {
[0] = "name"
[1] = 23
[2] = "age"
}
[1] = 2 values {
[0] = true
[1] = "sex"
}
[2] = 2 values {
[0] = "weight"
[1] = "height"
}
}

字典嵌套

27> let sumDic = ["key1":array1,"key2":array2,"key3":array3]
sumDic: [String : [Any]] = 3 key/value pairs {
[0] = {
key = "key2"
value = 2 values {
[0] = true
[1] = "sex"
}
}
[1] = {
key = "key3"
value = 2 values {
[0] = "weight"
[1] = "height"
}
}
[2] = {
key = "key1"
value = 3 values {
[0] = "name"
[1] = 23
[2] = "age"
}
}
}

查询

28> sumDic["key1"]
$R10: [Any]? = 3 values {
[0] = "name"
[1] = 23
[2] = "age"
}

创建空数组 普通方式

29> var myEmptyArray : Array = []
myEmptyArray: [Int] = 0 values
简洁方式
30> var myEmptyArray = Any
myEmptyArray: [Any] = 0 values

给数组赋空值,删除所有元素

31> myEmptyArray += ["1",true,2]
32> myEmptyArray
$R11: [Any] = 3 values {
[0] = "1"
[1] = true
[2] = 2
}

33> myEmptyArray = []
34> myEmptyArray
$R12: [Any] = 0 values

创建空字典

35> var myEmptyDic = Dictionary()
myEmptyDic: [String : Double] = 0 key/value pairs
36> var myEmptyDic = Int: String
myEmptyDic: [Int : String] = 0 key/value pairs

声明后在清空

37> myEmptyDic = [:]
38> myEmptyDic
$R13: [Int : String] = 0 key/value pairs

迭代数组

39> for arr in sumArray{
40. print("(arr)")
41. }
["name", 23, "age"]
[true, "sex"]
["weight", "height"]

42> for (index , arr) in sumArray.enumerated(){
43. print("(arr)is in (index) of the array")
44. }
["name", 23, "age"]is in 0 of the array
[true, "sex"]is in 1 of the array
["weight", "height"]is in 2 of the array

迭代字典

45> for (key , value) in sumDic{
46. print("(key) and (value)")
47. }
key2 and [true, "sex"]
key3 and ["weight", "height"]
key1 and ["name", 23, "age"]

遍历所有值

54> for (value) in sumDic.values{
55. print("(key)")
56. }
[true, "sex"]
["weight", "height"]
["name", 23, "age"]

遍历所有键

51> for (key) in sumDic.keys{
52. print("(key)")
53. }
key2
key3
key1

© 著作权归作者所有

共有 人打赏支持
上一篇: 3流程控制
下一篇: 1.Swift 简介
阿元
粉丝 5
博文 163
码字总数 108443
作品 0
浦东
程序员
私信 提问
Groovy(四)-布尔值,Lists,Arrays,Maps

译文:Groovy Language Documentation 布尔值 布尔值是一种用来表示真值:true和false的特殊数据类型。用这种数据类型可以设置简单的标志位来跟踪true/false情况。 布尔类型可以保存在变量中...

ZJ_Rocky
2017/12/06
0
0
Java:Collection集合、泛型

1、Collection集合-概述 1)、集合是一个装”引用类型”的”容器” 2)、Java内部提供了很多”集合类”,每种集合类对元素的存储采用了不同的”数据结构”–集合存储数据的方式。 3)、这些数...

烟火式Java
09/13
0
0
Java集合框架(一)——集合概述

本文概述 本篇文章将分三块内容对Java中的集合框架进行介绍: 一. 集合框架相关概念 二. 集合体系通用方法 三. 集合遍历—Iteractor 一. 集合框架相关概念 集合:用于存储多个对象的容器 1....

Mr_Yanger
2017/11/11
0
0
集合遍历:for VS foreach

假设我们拥有下面的集合 1 System.Collections.ArrayList list = new System.Collections.ArrayList(); 2 list.Add( 1 ); 3 list.Add( 12 ); 4 list.Add( 3 ); 5 list.Add( 56 ); 现在我们要......

彭博
2012/03/09
186
0
Java集合框架的知识总结(1)

说明:先从整体介绍了Java集合框架包含的接口和类,然后总结了集合框架中的一些基本知识和关键点,并结合实例进行简单分析。 1、综述 所有集合类都位于java.util包下。集合中只能保存对象(保...

dong.li
2012/04/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

C4C销售订单行项目价格维护方法

需求很简单,能够创建销售订单,在行项目里添加产品,带出价格来,同时把总价显示在销售订单抬头区域。 如下图所示: 下面是具体配置。 Business Configuration里,点击Sales Order的配置: ...

JerryWang_SAP
11分钟前
0
0
deepin中配置robot framework环境

本文永久更新地址:https://my.oschina.net/bysu/blog/2989005 【若要到岸,请摇船:开源中国 不最醉不龟归】 1.在终端中输入pip,回车,如果提示没有该命令,则先安转pip sudo apt-get inst...

不最醉不龟归
57分钟前
6
0
OSChina 周日乱弹 —— 钱不还,我就当你人不在了

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @莱布妮子 :分享Bigleaf的单曲《小鹿》 《小鹿》- Bigleaf 手机党少年们想听歌,请使劲儿戳(这里) 周日在家做什么? 做手工呀, @poorfis...

小小编辑
今天
154
5
EOS docker开发环境

使用eos docker镜像是部署本地EOS开发环境的最轻松愉快的方法。使用官方提供的eos docker镜像,你可以快速建立一个eos开发环境,可以迅速启动开发节点和钱包服务器、创建账户、编写智能合约....

汇智网教程
今天
22
0
《唐史原来超有趣》的读后感优秀范文3700字

《唐史原来超有趣》的读后感优秀范文3700字: 作者:花若离。我今天分享的内容《唐史原来超有趣》这本书的读后感,我将这本书看了一遍之后就束之高阁了,不过里面的内容一直在在脑海中回放,...

原创小博客
今天
32
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部