如果赋值创建数组到一个变量,它总是可变的,这意味着可以通过添加元素来改变它, 删除或更改其项目,但如果分配一个数组常量到则该数组,则数组是不可被改变的, 也就它的大小和内容不能被改变。
创建数组
可以使用下面的初始化程序语法来创建某种类型的空数组:
var arr = ["12","12","12"]
访问数组
可以使用下标语法从数组中检索对应值,传递数组名后方括号内的索引对应的值,如下:
var a = arr[0]
在这里,指数从0开始,这意味着可以使用索引0来访问第一个元素,第二元素可以通过使用索引1进行访问,其它类似。让我们来看看下面创建,初始化和访问数组的例子:
import Cocoa
var someInts = [Int](count: 3, repeatedValue: 10)
var someVar = someInts[0]
println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )
当上述代码被编译和执行时,它产生了以下结果:
Value of first element is 10
Value of second element is 10
Value of third element is 10
修改数组
可以使用 append() 方法或加法赋值运算符(+=)将新的项目添加到数组的末尾,在这里首先创建一个空的数组,然后添加新的元素到数组中,如下图所示:
import Cocoa
var someInts = [Int]()
someInts.append(20)
someInts.append(30)
someInts += [40]
var someVar = someInts[0]
println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )
当上述代码被编译和执行时,它产生了以下结果:
Value of first element is 20
Value of second element is 30
Value of third element is 40
可以通过分配一个在给定的索引处新的值修改数组的现有元素,如在下面的例子:
import Cocoa
var someInts = [Int]()
someInts.append(20)
someInts.append(30)
someInts += [40]
// Modify last element
someInts[2] = 50
var someVar = someInts[0]
println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )
当上述代码被编译和执行时,它产生了以下结果:
Value of first element is 20
Value of second element is 30
Value of third element is 50
迭代/遍历数组
可以使用 for-in 循环迭代级数,在下面的例子是数组的整个集值,如下图所示:
import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for item in someStrs {
println(item)
}
当上述代码被编译和执行时,它产生了以下结果:
Apple
Amazon
Google
也可以使用 enumerate() 函数,如下面的例子所示,它返回索引及对应的值:
import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for (index, item) in enumerate(someStrs) {
println("Value at index = \(index) is \(item)")
}
当上述代码被编译和执行时,它产生了以下结果:
Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google
两个数组相加
使用加法运算符(+),以添加的相同类型的数组,这将产生新的数组是来自两个数组值相加组合后的数组,如下:
import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = intsA + intsB
for item in intsC {
println(item)
}
当上述代码被编译和执行时,它产生了以下结果:
2
2
1
1
1
count 属性
可以使用只读计算 (count) 数组属性,找出下面显示出数组中元素的个数:
import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = intsA + intsB
println("Total items in intsA = \(intsA.count)")
println("Total items in intsB = \(intsB.count)")
println("Total items in intsC = \(intsC.count)")
当上述代码被编译和执行时,它产生了以下结果:
Total items in intsA = 2
Total items in intsB = 3
Total items in intsC = 5
空属性
使用只读数组的空属性(isEmpty)找出一个数组是否为空,如下图所示:
import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = [Int]()
println("intsA.isEmpty = \(intsA.isEmpty)")
println("intsB.isEmpty = \(intsB.isEmpty)")
println("intsC.isEmpty = \(intsC.isEmpty)")
当上述代码被编译和执行时,它产生了以下结果:
intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true