标签:
// 数组的用法
let empetyArray = [] // 空数组
let stringArray = [String]() // 字符串数组,限定其值为字符串
let intArray = [Int]() // 整型数组,限定其值为整型
var shoppingList = ["苹果","香蕉","西瓜","木瓜","黄瓜","冬瓜"] // 声明一个四个字符串的数组
print(shoppingList.capacity)
// 增加
shoppingList.append("芒果") // 方法一
shoppingList += ["荔枝"]; // 方法二
shoppingList.insert("橘子", atIndex: 0) // 方法三
print(shoppingList)
// 常用用法
print(shoppingList.count) // 数组中元素的个数
print(shoppingList.capacity) // 数组的容量最大为12
print(shoppingList.isEmpty) // 判断数组是否为空
// 修改
shoppingList[1] = "哈密瓜"
print(shoppingList)
shoppingList[2...5] = ["Apples","Banans"] // 把下标为2、3、4、5的值改为 "Apples","Banans"
print(shoppingList)
// 删除
shoppingList.removeLast()
print(shoppingList)
shoppingList.removeAtIndex(3)
print(shoppingList)
//shoppingList.removeAll()
//print(shoppingList)
// 数组的遍历
for item in shoppingList
{
print(item)
}
标签:
原文地址:http://www.cnblogs.com/fantasy3588/p/5068360.html