您好,欢迎访问一九零五行业门户网

java判断指定元素是否包含数组中的方法介绍

如何检查一个数组(无序)是否包含一个特定的值?这是一个在java中经常用到的并且非常有用的操作。(推荐:java视频教程)
下面我们来看一下java中判断数组中是否包含指定元素的方法:
检查数组是否包含某个值的方法
1、使用list
public static boolean uselist(string[] arr, string targetvalue) {    return arrays.aslist(arr).contains(targetvalue);}
2、使用set
public static boolean useset(string[] arr, string targetvalue) {    set<string> set = new hashset<string>(arrays.aslist(arr));    return set.contains(targetvalue);}
3、使用循环判断
public static boolean useloop(string[] arr, string targetvalue) {    for(string s: arr){        if(s.equals(targetvalue))            return true;    }    return false;}
4、使用arrays.binarysearch()
arrays.binarysearch()方法只能用于有序数组!!!如果数组无序的话得到的结果就会很奇怪。
查找有序数组中是否包含某个值的用法如下:
public static boolean usearraysbinarysearch(string[] arr, string targetvalue) {     int a =  arrays.binarysearch(arr, targetvalue);    if(a > 0)        return true;    else        return false;}
更多java知识请关注java基础教程栏目。
以上就是java判断指定元素是否包含数组中的方法介绍的详细内容。
其它类似信息

推荐信息