Array的用法總結-swift

 

Araay是有序的數據集,在OC中分爲不可變數組NSArray和可變數組NSMutableArray,在swift中只有常量和變量兩種類型,聲明成變量那就能夠說明是可變的了!swift

 

學習時的具體的用法總結成以下的代碼:數組

 

 //數組
        var arrInts = [Int]()//建立一個空數組
        arrInts = []; print("arrInts is of type [Int] with \(arrInts.count) items.")//// 打印 "someInts is of type [Int] with 0 items."
 var threeDoubles = Array(repeating:0.0,count:3)//建立一個帶有默認值的數組
        print("threeDoublesArray:\(threeDoubles)")//打印 threeDoublesArray:[0.0, 0.0, 0.0]
        threeDoubles += threeDoubles//數組合並
        print("threeDoubles:\(threeDoubles)")//打印 threeDoubles:[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 let anotherThreeDoubles = Array(repeating: 2.5, count: 3) let sixDoubles = threeDoubles + anotherThreeDoubles;//數組合並
        print(sixDoubles)//打印 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.5, 2.5, 2.5] //用數組字面量構造數組
        var goodsListArr:[String] = ["onions","eggs","apple","orange","pear","salt"] goodsListArr.append("vinegar")//在數組的末尾添加一個元素(不能夠用下標訪問的形式去在數組尾部添加新項)
        goodsListArr += ["Chocolate Spread", "Cheese", "Butter"]//給數組添加幾個元素
        
        if goodsListArr.isEmpty {//判斷數組是否爲空
            print("The shopping list is empty.") } else { print("The shopping list is not empty.") } // 打印 "The shopping list is not empty."(shoppinglist 不是空的)
 let firstItem = goodsListArr[0]//根據索引 取對應的索引值
        print("firstItemValue:\(firstItem)")//打印 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", "vinegar", "Chocolate Spread", "Cheese", "Butter"]
        goodsListArr[2...4] = ["Bananas", "Apples"]//將某個範圍的值替換掉
        print("results of substitution:\(goodsListArr)")//替換後的結果 results of substitution:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese", "Butter"] //在數組中插入元素(調用數組的insert(_:at:)方法來在某個具體索引值以前添加數據項)
        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"] //若是咱們試着對索引越界的數據進行檢索或者設置新值的操做,會引起一個運行期錯誤。咱們能夠使用索引值和數組的count屬性進行比較來在使用某個索引以前先檢驗是否有效。除了當count等於 0 時(說明這是個空數組),最大索引值一直是count - 1,由於數組都是零起索引
 let lastItem = goodsListArr.removeLast()//將數組的最後一個元素移除並獲取被移除的最後一個元素值
 print("removed last item is:\(lastItem) After removing the results:\(goodsListArr)")//打印 removed last item is:Butter After removing the results:["eight onions", "eggs", "Bananas", "Apples", "salt", "vinegar", "Chocolate Spread", "Cheese"]
        
        
        for item in goodsListArr{//數組的遍歷
 print("數組遍歷的結果:\(item)") /* 打印 數組遍歷的結果:eight onions 數組遍歷的結果:eggs 數組遍歷的結果:Bananas 數組遍歷的結果:Apples 數組遍歷的結果:salt 數組遍歷的結果:vinegar 數組遍歷的結果:Chocolate Spread 數組遍歷的結果:Cheese */ } //使用enumerated()方法來進行數組遍歷。enumerated()返回一個由每個數據項索引值和數據值組成的元組。咱們能夠把這個元組分解成臨時常量或者變量來進行遍歷(能夠同時d獲得每一個數據項的值和索引值)
        for(index,value) in goodsListArr.enumerated(){ print("Item \(String(index + 1)): \(value)") /*打印 Item 1: eight onions Item 2: eggs Item 3: Bananas Item 4: Apples Item 5: salt Item 6: vinegar Item 7: Chocolate Spread Item 8: Cheese */ }


這是我近期在學習swift的學習總結,給朋友們提供學習參考,同時發現有錯誤的地方能夠指出相互交流學習共同進步!app

相關文章
相關標籤/搜索