java数组包含某个值
如何检查Java数组是否包含值? (How to Check if Java Array Contains a Value?)
There are many ways to check if a Java array contains a specific value.
有很多方法可以检查Java数组是否包含特定值。
- Simple iteration using for loop 使用for循环的简单迭代
- List contains() method 列出contains()方法
- Stream anyMatch() method 流anyMatch()方法
- Arrays binarySearch() for sorted array 数组binarySearch()用于排序的数组
Let’s look into all these methods one at a time.
让我们一次研究所有这些方法。
1.使用For循环 (1. Using For Loop)
This is the easiest and convenient method to check if the array contains a certain value or not. We will go over the array elements using the for loop and use the equals() method to check if the array element is equal to the given value.
这是检查数组是否包含特定值的最简便方法。 我们将使用for循环遍历数组元素,并使用equals()方法检查数组元素是否等于给定值。
String[] vowels = { "A", "I", "E", "O", "U" };
// using simple iteration over the array elements
for (String s : vowels) {
if ("E".equals(s)) {
System.out.println("E found in the vowels list.");
}
}
2.使用List contains()方法 (2. Using List contains() method)
We can use Arrays class to get the list representation of the array. Then use the contains() method to check if the array contains the value. Let’s use JShell to run the example code snippet.
我们可以使用Arrays类来获取数组的列表表示形式。 然后使用contains()方法检查数组是否包含该值。 让我们使用JShell运行示例代码片段。
jshell> String[] vowels = { "A", "I", "E", "O", "U" };
vowels ==> String[5] { "A", "I", "E", "O", "U" }
jshell> List
vowelsList = Arrays.asList(vowels);
vowelsList ==> [A, I, E, O, U]
jshell> vowelsList.contains("U")
$3 ==> true
jshell> vowelsList.contains("X")
$4 ==> false
3.使用Stream anyMatch()方法 (3. Using Stream anyMatch() Method)
If you are using Java 8 or higher, you can create a stream from the array. Then use the anyMatch() method with a lambda expression to check if it contains a given value.
如果使用Java 8或更高版本,则可以从数组创建流 。 然后将带lambda表达式的anyMatch()方法用于检查它是否包含给定值。
jshell> List
vowelsList = Arrays.asList(vowels);
vowelsList ==> [A, I, E, O, U]
jshell> Arrays.stream(vowels).anyMatch("O"::equals);
$5 ==> true
jshell> Arrays.stream(vowels).anyMatch("X"::equals);
$6 ==> false
4.数组binarySearch()用于排序的数组 (4. Arrays binarySearch() for sorted array)
If your array is sorted, you can use the Arrays binarySearch() method to check if the array contains the given value or not.
如果对数组进行了排序,则可以使用Arrays binarySearch()方法检查数组是否包含给定值。
String[] vowels = { "A", "I", "E", "O", "U" };
System.out.println("Unsorted Array = " + Arrays.toString(vowels));
Arrays.parallelSort(vowels);
System.out.println("Sorted Array = " + Arrays.toString(vowels));
int index = Arrays.binarySearch(vowels, "X");
if (index < 0) {
System.out.println("X not found in the array");
} else {
System.out.println("X found in the array");
}
Output:
输出 :
Unsorted Array = [A, I, E, O, U]
Sorted Array = [A, E, I, O, U]
X not found in the array
检查数组是否包含多个值 (Checking if Array Contains Multiple Values)
What if we want to check if the array contains multiple values. Let's say you want to check if a given array is the subset of the source array. We can create nested loops and check each element one by one. There is a cleaner way by converting arrays to list and then use the containsAll() method.
如果我们要检查数组是否包含多个值怎么办。 假设您要检查给定的数组是否是源数组的子集。 我们可以创建嵌套循环并逐个检查每个元素。 有一种更干净的方法,将数组转换为列表,然后使用containsAll()方法。
String[] vowels = { "A", "I", "E", "O", "U" };
String[] subset = { "E", "U" };
boolean foundAll = Arrays.asList(vowels).containsAll(Arrays.asList(subset));
System.out.println("vowels contains all the elements in subset = " + foundAll);
Output: vowels contains all the elements in subset = true
输出 : 元音包含subset = true中的所有元素
参考资料 (References)
翻译自: https://www.journaldev.com/32438/java-array-contains-value
java数组包含某个值