iOS开发 - 「Swift 学习」Array集合类型创建、编辑
2021-11-20 本文已影响0人
俺不是大佬儿
Swift语言创建数组,合并、插入、删除数组元素
创建数组
一、创建空数组
//创建item类型为int的空数组
var intsArr = [Int]()
intsArr = [];
print("intsArr is of type [Int] with \(intsArr.count) items.")
//打印输出:intsArr is of type [Int] with 0 items.
二、创建数值型数组
直接用字面量创建数组类型,数组字面量是一系列由逗号分割并由方括号包含的数值:
//创建一个有3个double类型值的数组[0.0,.0.0,0.0]
var threeDoubles1:[Double] = [0.0,0.0,0.0]
var threeDoubles2 = Array(repeating: 0.0, count: 3)
print("threeDoubles1 = \(threeDoubles1)\n threeDoubles2 = \(threeDoubles2)")
//打印输出:
/*
threeDoubles1 = [0.0, 0.0, 0.0]
threeDoubles2 = [0.0, 0.0, 0.0]
*/
//直接用字面量创建数组类型
var goodsListArr:[String] = ["onions","eggs","apple","orange","pear","salt"]
//判断数组是否为空
if goodsListArr.isEmpty {
print("The shopping list is empty.")
}else {
print("The shopping list is not empty.")
}
//打印输出:
//The shopping list is not empty.
数组编辑
一、数组合并
可以使用加法操作符(+)来组合两种已存在的相同类型数组
。新数组的数据类型会被从两个数组的数据类型中推断出来
:
//数组合并
threeDoubles1 += threeDoubles1
print("数组合并结果:\(threeDoubles1)")
//打印输出:
//数组合并结果:[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
let anotherThreeDoubles = Array(repeating: 2.5, count: 3)
let sixDoubles = threeDoubles2 + anotherThreeDoubles;//数组合并
print("合并结果:\(sixDoubles)")
//打印输出:
//合并结果:[0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
二、替换数组索引值
数组索引
如果我们试着对索引越界的数组进行检索或者设置新值的操作,会引发一个运行期错误。我们
可以使用索引值和数组的count属性进行比较来在使用某个索引之前先检验是否有效
。除了当count等于 0 时(说明这是个空数组),最大索引值一直是count - 1
,因为数组都是零起索引
//数组索引
let arrFirstItem = goodsListArr[0]//根据索引 取对应的索引值
print("firstItemValue:\(arrFirstItem)")
//打印输出: firstItemValue:onions
替换索引值
//替换数组索引值
goodsListArr[0] = "eight onions"//将第一个索引值替换掉
// 其中的第一项现在是 "Six onions" 而不是 "onions"
print("Replace the former results:\(goodsListArr)")
//替换后的结果:
//Replace the former results:["eight onions", "eggs", "apple", "orange", "pear", "salt"]
替换某个范围的索引值
//将某个范围的值替换掉
//将2、3、4索引上的3个值("apple", "orange", "pear") 替换 成("Bananas", "Apples")
goodsListArr[2...4] = ["Bananas", "Apples"]
print("results of substitution:\(goodsListArr)")
//替换后的结果:
//results of substitution:["eight onions", "eggs", "Bananas", "Apples", "salt"]
三、数组插入元素
//在数组的末尾添加一个元素(不可以用下标访问的形式去在数组尾部添加新项)
goodsListArr.append("vinegar")
//给数组添加多个元素(从尾部添加)
goodsListArr += ["Chocolate Spread", "Cheese", "Butter"]
//在数组中插入元素,调用数组的insert(<#T##newElement: String##String#>, at: <#T##Int#>)方法来在某个具体索引值之前添加数据项
goodsListArr.insert("books", at: 0)//在0索引之前添加数据,现在数组第一个元素是“books”
四、删除数组元素
根据索引删除
//根据索引移除数组中某一个元素
let removeItem = goodsListArr.remove(at: 0)//将数组的第一个元素移除并获取被移除的第一项元素
print("removed index 0 item is:\(removeItem) After removing the results:\(goodsListArr)")
//打印输出:
//removed index 0 item is:books After removing the results:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"]
删除第一个元素值
//将数组的第一个元素移除并获取被移除的第一个元素值
let removeFirstItem = goodsListArr.removeFirst()
print("removed first item is:\(removeFirstItem) After removing the results:\(goodsListArr)")
//打印输出:
//removed first item is:eight onions After removing the results:["eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"]
删除最后一个元素值
//将数组的最后一个元素移除并获取被移除的最后一个元素值
let removeLastItem = goodsListArr.removeLast()
print("removed last item is:\(removeLastItem) After removing the results:\(goodsListArr)")
//打印输出:
//removed last item is:Butter After removing the results:["eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese"]