04:swift-数组、字典、集合

问题

 

目录

 

预备

 

正文

 数组、集合、字典

/*测试数组
     可变数组
     不可变数组
     1.1:数组长度
     1.2: 数组判空
     1.3: 添加元素 append
     1.4 +=  可以使用加赋值运算符
     1.5: 索引访问值
     1.6: 在指定索引插入元素
     1.7:删除指定索引的值
     1.8:数组的遍历
     */
    func testArray() {
        // 1: 集合的可变性 如果你创建一个数组、合集或者一个字典,并且赋值给一个变量,那么创建的集合就是可变的。这意味着你随后可以通过添加、移除、或者改变集合中的元素来改变(或者说异变)集合。如果你把数组、合集或者字典赋值给一个常量,则集合就成了不可变的,它的大小和内容都不能被改变。
        // 不可变数组
        let array1: [String] = ["name1"]
        
        // 可变数组
        var array2: [String] = ["name2-0"]
        array2.append("name2-1")
        
        print(array1, array2)
        
        // 1.1:数组长度
        print(array2.count)
        
        // 1.2: 数组判空
        if array2.isEmpty {
           print("数组为空")
        } else {
           print("数组不为空")
        }
        
        // 1.3: 添加元素 append
        array2.append("name1-3")
        print("append: \(array2)")

        // 1.4 +=  可以使用加赋值运算符 ( +=)来在数组末尾添加一个或者多个同类型元素:
        array2 += ["Chocolate Spread", "Cheese", "Butter"]
        
        print("+=: \(array2)")

        // 1.5: 索引访问值
        print("索引访问值:\(array2[0])")
        
        // 1.6: 在指定索引插入元素
        array2.insert("name1-4", at: 0)
        print("在指定索引插入元素:\(array2)")

        // 1.7:删除指定索引的值
        let removeValue = array2.remove(at: 0)
        print("删除指定索引的值:\(removeValue)")
        
        // 1.8:数组的遍历
        for value in array2 {
            print(value)
        }
        
        // 有索引的遍历
        for (index, value) in array2.enumerated() {
            print("索引: \(index + 1)->值: \(value)")
        }
    }

    /* 测试集合 合集将同一类型且不重复的值无序地储存在一个集合当中。当元素的顺序不那么重要的时候你就可以使用合集来代替数组,或者你需要确保元素不会重复的时候。
     
     1: 类型哈希
     2:声明集合
     3:set长度
     4:判空
     5:set插入
     6:set删除
     7:set遍历
     */
    func testSet() {
        print("集合")
        // 1:为了能让类型储存在合集当中,它必须是可哈希的——就是说类型必须提供计算它自身哈希值的方法。哈希值是Int值且所有的对比起来相等的对象都相同,比如 a == b,它遵循 a.hashValue == b.hashValue。
        
        // 2:字面量声明Set
        var set1: Set<String> = []
        set1.insert("set0")
        set1.insert("set1")
        print(set1)
        
        // 3:Set长度
        print(set1.count)
        
        // 4:Set判空
        if set1.isEmpty {
            print("set为空")
        } else {
            print("set不为空")
        }
        
        // 5:插入set
        set1.insert("set2")
        print(set1)
        
        // 6: 删除set
        if let setElement = set1.remove("set3") {
            print("\(setElement)  删除成功")
        } else {
            print("删除set失败")
        }
        
        // 7:判断包含 contain 元素
        if set1.contains("set3") {
            print("包含")
        } else {
            print("不包含")
        }
        
        // 8:遍历
        for genre in set1 {
            print("\(genre)")
        }
    }
    
    /* 测试字典 合集将同一类型且不重复的值无序地储存在一个集合当中。当元素的顺序不那么重要的时候你就可以使用合集来代替数组,或者你需要确保元素不会重复的时候。
     
     1:字面量创建字典
     2: 添加数据
     3:访问和修改字典
     4:判空
     5: 移出键值对
     6: 遍历字典
     7: 遍历键
     */
    func testDic() {
        // 1:字面量创建字典
        var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
        
        // 2: 添加数据
        airports["LHR"] = "London"
        
        // 3:访问和修改字典
        print("\(airports["YYZ"] ?? "")")

        // 4:判空
        if airports.isEmpty {
            print("The airports dictionary is empty.")
        } else {
            print("The airports dictionary is not empty.")
        }
        
        // 5: 移出键值对
        if let removedValue = airports.removeValue(forKey: "DUB") {
            print("The removed airport's name is \(removedValue).")
        } else {
            print("The airports dictionary does not contain a value for DUB.")
        }
        
        // 6: 遍历字典
        // 元组遍历
        for (airportCode, airportName) in airports {
            print("\(airportCode): \(airportName)")
        }
        
        // 遍历键
        for airportCode in airports.keys {
            print("Airport code: \(airportCode)")
        }
         
        // 遍历值
        for airportName in airports.values {
            print("Airport name: \(airportName)")
        }
    }

 

注意

 

引用

posted on 2023-02-23 11:44  风zk  阅读(22)  评论(0编辑  收藏  举报

导航