Array的用法总结-swift

 

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的学习总结,给朋友们提供学习参考,同时发现有错误的地方可以指出相互交流学习共同进步!

 

posted @ 2017-06-06 10:44  #零下一度&  阅读(2743)  评论(0编辑  收藏  举报