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

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

 

  数组是存储有序的相同类型的集合,相同的值可以多次出现在不同的位置。

注意:

  swift的Array类型桥接Foundation的NSArray类

 

  数组类型简单语法

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

 

  一、创建数组

  1.创建一个空数组

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

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.创建一个带有默认值的数组

  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"]

 

  二、数组的访问和修改

   我们可以通过数组的方法和属性来访问和修改数组,或者下标语法。 使用数组的只读属性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(),因为它不用查询数组的长度。

 

  四、数组的遍历

  你可以使用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”

 

posted @ 2016-04-16 03:45  ForrestWoo  阅读(911)  评论(0编辑  收藏  举报