如何檢查一個元素是否在數(shù)組中?含代碼

    如何檢查一個元素是否在數(shù)組中?

    本文將解釋如何在Swift語言中檢查一個元素是否存在于數(shù)組中。愛掏網(wǎng) - it200.com

    在Swift中有幾種方法可以檢查一個元素是否在數(shù)組中 –

    contains(_:)方法在數(shù)組中包含目標元素時返回true。愛掏網(wǎng) - it200.com這個方法只適用于元素符合Equatable協(xié)議的數(shù)組。愛掏網(wǎng) - it200.com

    這里是一個使用包含數(shù)組的字符串的例子。愛掏網(wǎng) - it200.com字符串符合Equatable協(xié)議,所以我們可以在此處使用contains方法。愛掏網(wǎng) - it200.com

    算法

    • 第1步 – 創(chuàng)建一個包含字符串的輸入數(shù)組

    • 第2步 – 通過傳遞目標元素來調(diào)用contains()方法

    • 第3步 – contains()方法返回一個布爾值

    示例

    import Foundation
    let languages = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"]
    let targetElement = "Swift"
    print ("Given array =",languages,"\nElement to search =",targetElement)
    
    if languages.contains(targetElement) {
        print("\(targetElement) is in the array.")
    } else {
        print("\(targetElement) is not in the array.")
    }
    

    輸出

    Given array = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"] 
    Element to search = Swift
    Swift is in the array.
    

    使用 contains(where:) 方法

    contains(where:) 是 Swift 中 Sequence 協(xié)議的一個方法,它返回一個布爾值,指示序列是否包含滿足給定斷言的元素。愛掏網(wǎng) - it200.com

    算法

    • 步驟1 – 創(chuàng)建一個輸入字符串數(shù)組

    • 步驟2 – 調(diào)用 contains(where:) 方法

    • 步驟3 – 在 contains(where:) 函數(shù)的閉包中檢查目標元素

    • 步驟4 – contains(where:) 方法返回一個布爾值

    示例

    下面是一個示例,演示如何使用 contains(where:) 來檢查字符串數(shù)組是否包含一個字符串 ?

    import Foundation
    let languages = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"]
    let targetElement = "Swift"
    print ("Given array =",languages,"\nElement to search =",targetElement)
    if languages.contains(where: { $0 == targetElement }) {
        print("\(targetElement) is in the array.")
    } else {
        print("\(targetElement) is not in the array.")
    }
    

    輸出

    Given array = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"] 
    Element to search = Swift
    Swift is in the array.
    

    示例

    您還可以使用contains(where:)在自定義類型的數(shù)組中搜索元素,只要您提供一個返回布爾值的謂詞,該謂詞指示元素是否滿足某個條件。愛掏網(wǎng) - it200.com例如,

    import Foundation
    struct Student {
        let name: String
        let score: Int
    }
    let students: [Student] = [Student(name: "John", score: 80),
       Student(name: "Tina", score: 75),
       Student(name: "Bob", score: 89),
       Student(name: "Alice", score: 67)]
    print(students)                         
    
    if students.contains(where: { $0.score > 80 }) {
        print("The student array contains a student who scored more than 80%.")
    } else {
        print("The student array does not contain any students who scored more than 80%.")
    }
    

    輸出

    [main.Student(name: "John", score: 80), main.Student(name: "Tina", score: 75), main.Student(name: "Bob", score: 89), main.Student(name: "Alice", score: 67)]
    The student array contains a student who scored more than 80%.
    

    使用filter方法

    filter方法是Swift中的一種高階函數(shù),它允許您通過僅包含滿足某個條件的元素來創(chuàng)建一個新數(shù)組。愛掏網(wǎng) - it200.com

    算法

    • 第1步 – 創(chuàng)建一個字符串的輸入數(shù)組

    • 第2步 – 調(diào)用filter()函數(shù)

    • 第3步 – 在filter()函數(shù)的閉包中檢查目標元素

    • 第4步 – filter()函數(shù)返回一個新的搜索元素數(shù)組

    • 第5步 – 檢查結(jié)果數(shù)組是否為空

    示例

    下面是一個示例,演示如何使用filter檢查一個元素是否在數(shù)組中:

    let languages = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"]
    let targetElement = "Swift"
    print ("Given array =",languages,"\nElement to search =",targetElement)
    let filteredElements = languages.filter({ $0 == targetElement })
    if filteredElements.isEmpty == false {
        print("\(targetElement) is in the array.")
    } else {
        print("\(targetElement) is not in the array.")
    }
    

    輸出

    Given array = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"] 
    Element to search = Swift
    Swift is in the array.
    

    使用firstIndex(of:)方法

    firstIndex(of:)是Swift中RandomAccessCollection協(xié)議的一個方法,它返回集合中第一個與給定元素相等的元素的索引,如果未找到該元素,則返回nil。愛掏網(wǎng) - it200.com

    以下是如何使用firstIndex(of:)在數(shù)組中查找特定元素的索引的示例 –

    算法

    • 步驟1 – 創(chuàng)建一個字符串的輸入數(shù)組

    • 步驟2 – 調(diào)用firstIndex()函數(shù)

    • 步驟3 – 如果目標元素包含在輸入數(shù)組中,則返回目標元素的索引

    • 步驟4 – firstIndex()函數(shù)返回第一個索引

    • 步驟5 – 檢查第一個索引是否有效

    示例

    import Foundation
    let languages = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"]
    let targetElement = "Swift"
    print ("Given array =",languages,"\nElement to search =",targetElement)
    if let index = languages.firstIndex(of: targetElement) {
        print("\(targetElement) is in the array at index \(index).")
    } else {
        print("\(targetElement) is not in the array.")
    }
    

    輸出

    Given array = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"] 
    Element to search = Swift
    Swift is in the array at index 4
    

    使用for循環(huán)

    算法

    • Step 1 – 創(chuàng)建一個字符串輸入數(shù)組

    • Step 2 – 對輸入數(shù)組執(zhí)行for循環(huán)

    • Step 3 – 檢查目標元素是否包含在輸入數(shù)組中

    • Step 4 – 如果找到目標元素,中斷for循環(huán)執(zhí)行

    示例

    import Foundation
    let languages = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"]
    let targetElement = "Swift"
    print ("Given array =",languages,"\nElement to search =",targetElement)
    for language in languages {
        if language == targetElement {
            print("\(targetElement) is in the array.")
            break
        }
    }
    

    輸出

    Given array = ["PHP", "Java", "Python", "JavaScript", "Swift", "Objective-C"] 
    Element to search = Swift
    Swift is in the array.
    

    結(jié)論

    我們有不同的方法來檢查一個元素是否在數(shù)組中。愛掏網(wǎng) - it200.com每種方法都有確定結(jié)果的時間復雜度。愛掏網(wǎng) - it200.com根據(jù)您的需求,您可以使用任何方法。愛掏網(wǎng) - it200.com

    聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。
    發(fā)表評論
    更多 網(wǎng)友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 亚洲一区二区三区影院| 久久久久成人精品一区二区| 美女福利视频一区| 欧美日韩综合一区二区三区| 爆乳熟妇一区二区三区霸乳| 亚洲AV一区二区三区四区 | 日韩精品无码人妻一区二区三区| 国产精品99精品一区二区三区 | 国产在线观看一区二区三区四区| 精品一区二区三区自拍图片区| 久久精品人妻一区二区三区| 亚洲一区二区三区无码国产| 久久毛片免费看一区二区三区| 国产精品污WWW一区二区三区| 无码精品久久一区二区三区| 亚洲日韩一区二区一无码| 蜜桃视频一区二区三区在线观看| 无码人妻AⅤ一区二区三区| 久久国产香蕉一区精品| 国产视频一区二区| 综合一区自拍亚洲综合图区| 中文字幕人妻第一区| 精品国产一区二区三区久久久狼| 国产乱码精品一区二区三区香蕉| 色狠狠色狠狠综合一区| 少妇激情av一区二区| 国产激情视频一区二区三区| www.亚洲一区| 少妇无码AV无码一区| 真实国产乱子伦精品一区二区三区 | 人妻视频一区二区三区免费| 无码人妻一区二区三区一| 精品无码一区在线观看| 亚洲大尺度无码无码专线一区 | 丰满爆乳一区二区三区| 亚洲日韩国产精品第一页一区| 日产亚洲一区二区三区| 国产福利91精品一区二区| 成人h动漫精品一区二区无码| 午夜无码一区二区三区在线观看| 亚洲无线码一区二区三区|