如何检查一个元素是否在数组中?
本文将解释如何在Swift语言中检查一个元素是否存在于数组中。
在Swift中有几种方法可以检查一个元素是否在数组中 –
使用contains方法
contains(_:)方法在数组中包含目标元素时返回true。这个方法只适用于元素符合Equatable协议的数组。
这里是一个使用包含数组的字符串的例子。字符串符合Equatable协议,所以我们可以在此处使用contains方法。
算法
- 第1步 – 创建一个包含字符串的输入数组
-
第2步 – 通过传递目标元素来调用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 协议的一个方法,它返回一个布尔值,指示序列是否包含满足给定断言的元素。
算法
- 步骤1 – 创建一个输入字符串数组
-
步骤2 – 调用 contains(where:) 方法
-
步骤3 – 在 contains(where:) 函数的闭包中检查目标元素
-
步骤4 – contains(where:) 方法返回一个布尔值
示例
下面是一个示例,演示如何使用 contains(where:) 来检查字符串数组是否包含一个字符串 −
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:)在自定义类型的数组中搜索元素,只要您提供一个返回布尔值的谓词,该谓词指示元素是否满足某个条件。例如,
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中的一种高阶函数,它允许您通过仅包含满足某个条件的元素来创建一个新数组。
算法
- 第1步 – 创建一个字符串的输入数组
-
第2步 – 调用filter()函数
-
第3步 – 在filter()函数的闭包中检查目标元素
-
第4步 – filter()函数返回一个新的搜索元素数组
-
第5步 – 检查结果数组是否为空
示例
下面是一个示例,演示如何使用filter检查一个元素是否在数组中:
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协议的一个方法,它返回集合中第一个与给定元素相等的元素的索引,如果未找到该元素,则返回nil。
以下是如何使用firstIndex(of:)在数组中查找特定元素的索引的示例 –
算法
- 步骤1 – 创建一个字符串的输入数组
-
步骤2 – 调用firstIndex()函数
-
步骤3 – 如果目标元素包含在输入数组中,则返回目标元素的索引
-
步骤4 – firstIndex()函数返回第一个索引
-
步骤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循环
算法
-
Step 1 – 创建一个字符串输入数组
-
Step 2 – 对输入数组执行for循环
-
Step 3 – 检查目标元素是否包含在输入数组中
-
Step 4 – 如果找到目标元素,中断for循环执行
示例
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.
结论
我们有不同的方法来检查一个元素是否在数组中。每种方法都有确定结果的时间复杂度。根据您的需求,您可以使用任何方法。