java数组包含某个值_如何检查Java数组是否包含值?

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)

  1. Arrays binarySearch() API Doc

    数组binarySearch()API文档
  2. Stream anyMatch() API Doc

    流anyMatch()API文档

翻译自: https://www.journaldev.com/32438/java-array-contains-value

java数组包含某个值

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值