## 2.使用集合 原

阿元

2.1数组

1array￼

``````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"
}
``````

``````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] = {
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]
``````

``````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] = {
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] = {
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
``````

### 阿元

Groovy(四)-布尔值，Lists，Arrays，Maps

ZJ_Rocky
2017/12/06
0
0
【转载】C#使用Except方法求取两个List集合的差集数据

05/12
0
0
【转载】C#编程中两个List集合使用Intersect方法求交集

05/12
0
0
Java：Collection集合、泛型

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

2018/09/13
0
0
Java集合框架（一）——集合概述

Mr_Yanger
2017/11/11
0
0

linux下ctrl+c中止不了

gaolongquan
26分钟前
1
0
Android三年工作面经

JerryLin123
29分钟前
2
0

32分钟前
7
0

49分钟前
1
0
【JavaScript】callee 与 caller

callee callee是函数参数arguments对象的一个属性，它指向参数arguments对象所在的函数自身。 function foo (x) {console.log(arguments.callee);return x;}foo(); 从控制台中可以看...

53分钟前
2
0