1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| 数组
性质: 1. 有序 2. 类型统一 3. 内容可重复
--------------------------------------------------------------------------------
var a = [1,2,3] var b = ["a","b","c"] var c = [true,false,true] print(a,b,c)
var a1:Array<Int> = [1,2,3] var a2:[Int] = [1,2] print(a1,a2)
var b1 = Array<Int>() var b2 = Array<Int>([1,2]) var b3 = Array(1...7) print(b1,b2,b3)
var c1 = Array(repeating:"*", count:6) print(c1) print(c1.count) print(c1.isEmpty)
--------------------------------------------------------------------------------
print(a[0]) print(b[0...2]) print(b[...2]) print(b[1...])
--------------------------------------------------------------------------------
for item in b { print(item, terminator: "-") } print("")
for (i, value) in b.enumerated() { print(i, value, separator: ":") }
print(b[b.count - 1]) print(b.first ?? "") print(b.last ?? "") print(b.randomElement() ?? "")
--------------------------------------------------------------------------------
var v = ["坚定", "坚持", "坚强"] v.append("努力") print(v)
v.append(contentsOf: ["勇敢","乐观"]) print(v)
v += ["奋斗","幸运"] print(v)
--------------------------------------------------------------------------------
v.insert("fjbq", at: 0) v.insert(contentsOf:["fj","fj777"], at: 0)
var v1 = Array<Int>() v1.insert(contentsOf: 1...3, at: 0) print(v1)
print(v) v.replaceSubrange(0...2, with:["1","2"]) print(v)
----------------------------------------------------------------------------------------
var v2 = ["111","222","333","444","111"] var v3 = Array<String>() var v4 = [111,222,333,444,111]
var flag = v2.contains("111")
var value = v2.first(where: {$0 == "222"}) print(value ?? "")
var value1 = v2.firstIndex(where: {n in n == "333" }) print(value1)
----------------------------------------------------------------------------------------
|