Swift,陣列

kimingw發表於2017-09-12

1、建立(Array)陣列(陣列內的型別一定要相同,有序的可重複)

(1)建立預設值的陣列

let array:[Int]
array=[Int](repeatElement(3,count:5))  //[3,3,3,3,3]

(2)建立有序範圍Int的陣列

let array=Array(0..<5)    //[0,1,2,3,4]
let array=Array(1...5)  //[1,2,3,4,5]

(3)字面量建立陣列(最常用)

let array=[1,2,3,4,5]  //[1,2,3,4,5]

(4)空陣列

var array=Array<Int>()  //[]

2、檢視陣列的長度(count)和判斷陣列是否為空(isEmpty)

let array=[1,2,3,4,5]
print(array.count)  //5
array.isEmpty  //false

3、陣列(append)最後插入

let array=["a","b"]
array.append("c")  //["a","b","c"]

4、陣列(insert)中間插入

let array=["a","b"]
array.insert("c",at:1)  //["a","c","b"]

5、根據陣列索引(remove)刪除

let array=["a","b"]
array.remove(at:1)  //["b"]

 

相關文章