swift學習之數組

來源:互聯網
上載者:User

標籤:lis   double   元素   2.0   替代   article   pread   取出   數組   

  首先數組的定義:以有序的方式儲存同樣類型的值

(1)數組的簡寫(shorthand)文法

你能夠通過Array<Element>,在這裡,Element時數組儲存元素的值的類型。也能夠通過中括弧來寫[Element]

(2)建立一個空數組

        var emptyArr = [Int]()(這裡使用初始化方法建立一個Int型的空數組)
        emptyArr.append(3)
        emptyArr = [] (這裡是用字面語句建立空數組)
note:emptyArr is now an empty array,but is still an type of [Int]

另外,就是我直接用var emptyArr = []建立一個空數組會產生一個錯誤,報了這是一個不可變數組,大家能夠試試

(3)建立一個帶有預設值的數組

swift提供了一種初始化一個數組。而且這個數組帶有同樣的預設值

    var defaultArr = [Double](count: 3, repeatedValue: 0.0)(實值型別是double型的)
    println(defaultArr)

//the println is [0.0, 0.0, 0.0]

(4)通過add兩個數組建立一個新的數組

通過(+)號操作將兩個已經存在的同樣類型的數組建立一個新的數組

        var anotherArr = [Double](count: 3, repeatedValue: 2.5)
        var newArr = defaultArr + anotherArr
        println(newArr)

//the result is [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

(5)用字面量建立一個數組

中括弧中面放一系列的值,以逗號分隔

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

這個聲明了一個僅僅儲存字元型的數組,由swift的類型判斷,我們能夠不用寫明類型用字面量文法初始化數組存放同樣類型的值時,因此,上面的範例能夠更加簡單點寫

       var shopping: = ["Eggs","Milk"]

(6)訪問和改變數組

我們學會了建立一個數組,那麼接下來就要想怎麼訪問一個數組,又怎麼改變一個數組呢?

訪問和改變數組,我們能夠通過方法和屬性,或者下標文法

我們能夠通過數組的僅僅讀屬性(.count)知道數組的元素個數

        println("The shopping list contains \(shoppingList.count) items")    (The shopping list contains 2 items)

我們能夠通過數組的Boolen屬性(.isEmpty)檢查數組的個數和0是否相等

if shoppingList.isEmpty{
            println("The shopping list is empty")
        }else{
            println("The shopping list isn‘t empty")
        }

我們能夠通過array‘s append(_:)方法在數組的最後面加入元素

        shoppingList.append("Flour")
        println(shoppingList)       ([Eggs, Milk, Flour])

另一種方法。我們也能夠通過(+=)操作加入一個或多個元素

        shoppingList += ["Baking Powder"]
        shoppingList += ["Chocolate Spread","Cheese","Butter"]
        println(shoppingList)  ([Eggs, Milk, Flour, Baking Powder, Chocolate Spread, Cheese, Butter])

能夠通過下標文法檢索一個數組的值,在數組名後面加上中括弧。在裡面寫上你想要的值所在的下標就能夠取出相應的下標值了,和oc一樣。第一個元素也是從0開始算起

       var firstItem = shoppingList[0]
        println(firstItem)  (firstItem is equal to "Eggs")

你也能夠通過下標文法改變相應索引處的值

        shoppingList[0] = "Six Eggs"
        println(shoppingList)   ([Six Eggs, Milk, Flour, Baking Powder, Chocolate Spread, Cheese, Butter])和上面對照確實改變了第一個元素

在swift中能夠通過下標文法改變一定範圍的值,即使替代的值的長度和你替代的範圍長度不同,如

        shoppingList[4...6] = ["Bananas","Apples"]
        println(shoppingList)

如今列印的結果是[Six Eggs, Milk, Flour, Baking Powder, Bananas, Apples]也就是數組有6個元素

        shoppingList[4...6] = ["Bananas","Apples","Bananas","Apples"]

如今數組有8個元素了

可是請注意你不能通過下標文法給數組加入元素

往數組中插入元素我們能夠通過調用insert(_:atIndex:)方法

        shoppingList.insert("Maple Syrup", atIndex: 0)
        println(shoppingList)      ([Maple Syrup, Six Eggs, Milk, Flour, Baking Powder, Bananas, Apples, Bananas, Apples])

相同的道理,能夠通過調用removeAtIndex(_:)方法移除元素。而且能夠接收被移除的元素(假如你不須要。就不用接收,直接忽略就好了)

        var removedItem = shoppingList.removeAtIndex(0)
        println(shoppingList)    ([Six Eggs, Milk, Flour, Baking Powder, Bananas, Apples, Bananas, Apples])

調用removeRange移除一定範圍內的數組

        var removedRangeItem: () = shoppingList.removeRange(6...7)
        println(shoppingList)      ([Six Eggs, Milk, Flour, Baking Powder, Bananas, Apples])

調用removeLast()文法移除數組最後一個元素。而不是通過調用removeAtIndex(_:)方法。以免遍曆數組一遍要

        shoppingList.removeLast()
        println(shoppingList)

數組的遍曆

我們能夠通過for-in迴圈遍曆

        println(shoppingList)
        for item in shoppingList{
            println(item)
(Six Eggs
Milk
Flour
Baking Powder
Bananas)
        }

假設你須要每一個元素的下標和相應的值,你須要用enumerate()方法來遍曆數組,這樣返回一個元組,包含元素的下標和相應的值

for (index,value) in enumerate(shoppingList){
            println("Item\(index):\(value)")
        }

Item0:Six Eggs
Item1:Milk
Item2:Flour
Item3:Baking Powder
Item4:Bananas
這是swift2.0之前的寫法,2.0之後用的是

        for (index,value) in shoppingList.enumerate(){
            println("Item\(index):\(value)")
        }

這種方法


借鑒:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105   

swift學習之數組

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.