swift—数组

数组的定义

var arr1 = [1,2,3,4]
var arr11:[Int] = [1,2,3,4]
var arr111:Array = [1,2,3,4]
var arr2:[Int] = []
var arr22 = [Int]()
var arr3 = [Int](repeatElement(0,count:10))
print(arr3)

基本操作

print(arr3.count)//获取数组长度
print(arr2.isEmpty) //判断是否为空
print(arr1[1])//下标从0开始
print(arr1[1...3])
print(arr1[1...])
print(arr1[1..<3])
print(arr1.first!)//获取数组第一个
print(arr1.last!)

遍历

//遍历1
for index in 0..

增删改查

// 增: 1)在末尾  2)在指定位置
var girlFriends = ["熊大","熊二","翠花","光头强"]
girlFriends += ["熊大"]
girlFriends.append("熊二")
girlFriends.insert("松鼠",at:0)
//删除: 1)删除单个  2)删除一片
girlFriends.remove(at:3)
girlFriends.removeSubrange(2..<5)
//改
girlFriends+=["翠花","关头强","熊二"]
girlFriends[0]=红色"
girlFriends[2..<4]=["哈哈"]
//查
print(girlFriends.contains("哈哈"))
print(girlFriends)

你可能感兴趣的:(swift—数组)