Arrays
var array = ["A","B","C","D"]
- 一个数组只能存储特定类型的数据
- 所存数据不一定是类的对象,也可以是基本数据类型
Array Type
- [String]
- Array<String>
声明数组的方法:
//声明数组的三种方式
var array1 = ["a","b","c","d","e"]
var array2:[String] = ["a","b","c","d","e"]
var array3:Array<String> = ["a","b","c","d","e"]
声明空的数组:
//数组的初始化
var array4 = [Int]()
var str4 = [String]()
var array5 = Array<String>()
数组的操作:
//拼接数组
array4 = Array<Int>(count: 10, repeatedValue: 0)//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var array6 = [1,2,3]
var array7 = array4 + array6//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3]
//遍历数组
for index in 0..<array.count
{
println(array[index])
}
for item in array{
println(item)
}