一,先转为List,再使用contains()方法
<pre> String[] strArr = new String[] { "a", "b", "c"};
String str = "c";
List list = Arrays.asList(strArr);
boolean result = list.contains(str);
System.out.println(result); // true</pre>
二,使用最基本的for循环
for循环的方法是效率最高的
<pre> String[] strArr = new String[] { "a", "b", "c" };
String str = "c";
for (int i = 0; i < strArr.length; i++) {
if (strArr[i].equals(str)) {
System.out.println("该元素在数组中: i=" + i); // 该元素在数组中: i=2
}
}</pre>
三,使用Apache Commons的ArrayUtils
Apache Commons类库有很多java判断数组是否包含,几乎大多数的开源框架都依赖于它,Commons中的工具会节省你大部分时间java判断数组是否包含,它包含一些常用的静态方法和Java的扩展。是开发中提高效率的一套框架.
<pre> String[] strArr = new String[] { "a", "b", "c" };
String str = "c";
boolean result = ArrayUtils.contains(strArr, str); // 推荐
System.out.println(result); // true</pre>
文章来源:https://blog.csdn.net/weixin_34004750/article/details/93658198