老猫swift学习笔记(三):数组

var arr:String[] = ["Allen","Jerry","Haley","Colin","Jay"]

println("size : \(arr.count)") //5

//往数组末尾添加数据Linda
arr += "Linda"

//往数组末尾添加数据Angel
arr.append("Angel")

//往数组末尾添加数组
arr += ["Alan","Adm"]

for item in arr {

    print("\(item) \t")
}
//print "Allen Jerry Haley Colin Jay Linda Angel Alan Adm"
println()

arr.removeAtIndex(2) //把Haley给删掉
var temp = arr.removeLast()//删除的元素 Adm
println(temp) //Adm

println(arr.isEmpty)//false

for item in arr {
    
    print("\(item) \t")
}
//print "Allen Jerry Colin Jay Linda Angel Alan"
println()

//修改数据
arr[2] = "Doctor"

for item in arr {
    
    print("\(item) \t")
}
//print "Allen Jerry Doctor Jay Linda Angel Alan "
println()

//修改多个
arr[2...3] = ["Update1","Update2"]
for item in arr {
    
    print("\(item) \t")
}
//print "Allen Jerry Update1 Update2 Linda Angel Alan "
println()

//修改多个,如果后面提供的元素不足,无法赋值的元素将被删除
arr[2...5] = ["AA","BB"] //2...5 需要4个元素,但这里只提供了两个,所以第四、第五哥元素将被删除
for item in arr {
    
    print("\(item) \t")
}
//print "Allen         Jerry         AA         BB         Alan"
println()

//好吧关于数组就到这里为止,剩下的大家自己去尝试下

 

上一篇:swift for-in循环中"..."与".."的区别

 

转载请标注:swift开发者www.swift-open.com

 

 

你可能感兴趣的:(ios,swift,swift社区,swift论坛)