【Swift学习】Swift编程之旅---集合类型之数组(六)

  swift提供了3种主要的集合类型,array,set,dictionary。本节介绍array。swift

 

  数组是存储有序的相同类型的集合,相同的值能够屡次出如今不一样的位置。数组

注意:app

  swift的Array类型桥接Foundation的NSArray类函数

 

  数组类型简单语法spa

  swift数组类型完整写做Array<Element>,Element是数组容许存储值的合法类型,你也能够简单的写做[Element]。尽管两种形式在功能上是同样的, 可是咱们推荐较短的那种,并且在本文中都会使用这种形式来使用数组。code

 

  1、建立数组blog

  1.建立一个空数组索引

须要注意的是,someInts变量的类型在初始化时推断为一个int类型,或者若是上下文已经提供类型信息,例如一个函数参数或者一个已经定义好类型的常量或者变量,咱们也能够直接写做 [],而不须要加Int。three

someInts.append(3) // someInts now contains 1 value of type Int someInts = [] // someInts is now an empty array, but is still of type [Int]

  

  3.建立一个带有默认值的数组rem

  Swift 中的Array类型还提供了一个能够建立特定大小而且全部数据设置为相同的默认值的构造方法。咱们能够把准备加入数组的item数量(count)和适当类型的初始值(repeatedValue)传入数组构造函数:

var threeDoubles = [Double](count: 3, repeatedValue: 0.0) // threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]

 

  4.2个数组合并为一个数组

  经过+来实现2个数组合为一个新数组

var anotherThreeDoubles = [Double](count: 3, repeatedValue: 2.5) // anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5] var sixDoubles = threeDoubles + anotherThreeDoubles // sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

 

  5.[value 1, value 2, value 3]

var shoppingList: [String] = ["Eggs", "Milk"]

 

这里说明shoppingList是一个存储String类型的数组变量,并且只能存储String类型的值。它还能够简单的写做

var shoppingList = ["Eggs", "Milk"]

 

  2、数组的访问和修改

   咱们能够经过数组的方法和属性来访问和修改数组,或者下标语法。 使用数组的只读属性count来获取数组的count。

print("The shopping list contains \(shoppingList.count) items.") // Prints "The shopping list contains 2 items.

 

  使用isEmpty判断数组是否为空。

 

  1.获取数组数据

  下标法

var firstItem = shoppingList[0] // firstItem is equal to "Eggs

 

  2.添加数据

  使用append(_:)方法在数组的结尾处添加一条新的数据。

shoppingList.append("Flour")

 

  使用+=也能够向数组中添加若干条新的数据

shoppingList += ["Baking Powder"] // shoppingList now contains 4 items shoppingList += ["Chocolate Spread", "Cheese", "Butter"] // shoppingList now contains 7 items

 

  使用insert(_:atIndex:)在指定位置插入新的数据

shoppingList.insert("Maple Syrup", atIndex: 0) // shoppingList now contains 7 items // "Maple Syrup" is now the first item in the list

 

  

  3.修改数组

  下标法修改其中一项值

shoppingList[0] = "Six eggs”

 

  也能够修改指定范围内的值

shoppingList[4...6] = ["Bananas", "Apples"] // shoppingList now contains 6 items

 

  咱们不能够经过下标法来在数组的末尾处添加新的数据,由于index超出数组长度后是不合法的,会发生运行时错误

 

  removeAtIndex(_:)删除数组中指定位置的数据

let mapleSyrup = shoppingList.removeAtIndex(0) // the item that was at index 0 has just been removed // shoppingList now contains 6 items, and no Maple Syrup // the mapleSyrup constant is now equal to the removed "Maple Syrup" string”

 

  若是你像删除数组中最后一条数据,那么你应该使用removeLast(),而不该该使用removeAtIndex(),由于它不用查询数组的长度。

 

  4、数组的遍历

  你可使用for-in'循环来遍历数组。

for item in shoppingList { print(item) }

 

若是咱们同时须要每一个数据项的值和索引值,可使用全局enumerate函数来进行数组遍历。enumerate返回一个由每个数据项索引值和数据值组成的键值对组。咱们能够把这个键值对组分解成临时常量或者变量来进行遍历:

for (index, value) in shoppingList.enumerate() { print("Item \(index + 1): \(value)") } // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas”
相关文章
相关标签/搜索