检查数组是否包含某个值的方法

检查数组是否包含某个值的方法

public class TestArray {
    //使用List
public static boolean useList(String[] arr,String targetValue){
    return Arrays.asList(arr).contains(targetValue);
}
//使用Set
public static boolean useSet(String[] arr,String targetValue){
    Set<String> set=new HashSet<String>(Arrays.asList(arr));
    return set.contains(targetValue);
}
//使用循环判断
public static boolean useLoop(String[] arr,String targetValue){
    for(String s:arr){
        if(s.equals(targetValue))
            return true;
        }  
        return false;
    }
//查找有序数组中是否包含某个值的用法
public static boolean useArraysBinarySearch(String[] arr,
                                            String targetValue){
    int a=Arrays.binarySearch(arr, targetValue);
    if(a>0)
        return true;
    else
        return false;
}
//使用ArrayUtils
public static boolean useArrayUtils(String[] arr,String targetValue){
    return ArrayUtils.contains(arr,targetValue);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容