如何确定数组是否包含 Java 中的特定值? [英] How do I determine whether an array contains a particular value in Java?
本文介绍了如何确定数组是否包含 Java 中的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 String[]
的值如下:
I have a String[]
with values like so:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
给定String s
,有没有什么好办法测试VALUES
是否包含s
?
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).
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);
要检查 int
、double
或 long
的数组是否包含值,请使用 IntStream
, DoubleStream
或 LongStream
分别.
To check whether an array of int
, double
or long
contains a value use IntStream
, DoubleStream
or LongStream
respectively.
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
这篇关于如何确定数组是否包含 Java 中的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文