我怎么能测试一个数组包含一定的价值? [英] How can I test if an array contains a certain value?
本文介绍了我怎么能测试一个数组包含一定的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个的String []
,象这样的值:
公共静态最后的String [] = VALUES新的String [] {AB,BC,CD,AE};
由于的String
,是否有检测是否值的好办法
包含取值
?
解决方案
Arrays.asList(yourArray)。载有(yourValue)
警告:这不会对原语的数组(参见评论)
工作Java的8
您现在可以使用流
来检查是否 INT
,的数组双
或长
包含值(分别使用 IntStream
, DoubleStream
或 LongStream
)
示例
INT [] A = {1,2,3,4};
布尔包含= IntStream.of(一).anyMatch(X - &X的催化剂== 4);
I have a String[]
with values like so:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
Given String s
, is there a good way of testing whether VALUES
contains s
?
解决方案
Arrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Since java-8
You can now use a Stream
to check whether an array of int
, double
or long
contains a value (by respectively using a IntStream
, DoubleStream
or LongStream
)
Example
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
这篇关于我怎么能测试一个数组包含一定的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文