Array的用法總結-swift

來源:互聯網
上載者:User

標籤:ring   屬性   變數   遍曆   other   bsp   rem   not   after   

 

Araay是有序的資料集,在OC中分為不可變數組NSArray和可變數組NSMutableArray,在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的學習總結,給朋友們提供學習參考,同時發現有錯誤的地方可以指出相互交流學習共同進步!

 

Array的用法總結-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.