【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」
相關文章
相關標籤/搜索