美文网首页
Java之判断数组中是否包含某个值

Java之判断数组中是否包含某个值

作者: 兰觅 | 来源:发表于2020-09-12 17:11 被阅读0次

方式一:使用Arrays.asList(str).contains()

public static boolean useList(String[] arr, String targetValue) {
        return Arrays.asList(arr).contains(targetValue);
    }

示例如下:

 String[] str={"学历教育","专业教育","通识教育","其它在职训"} ;
                if (!Arrays.asList(str).contains(excels.get(i).getTrainingType())){
                    return new BaseResult<>(BaseErrMsg.DB_INSERT_FAILURE,"第"+(i+1)+"行教育训练类别有误,请重新输入!");
                }

方式二:使用set.contains()

 public static boolean useSet(String[] arr, String targetValue) {
        Set<String> set = new HashSet<String>(Arrays.asList(arr));
        return set.contains(targetValue);
    }

方式三:使用for循环

public static boolean useLoop(String[] arr, String targetValue) {
        for (String s : arr) {
            if (s.equals(targetValue))
                return true;
        }
        return false;
    }

相关文章

网友评论

      本文标题:Java之判断数组中是否包含某个值

      本文链接:https://www.haomeiwen.com/subject/sllbektx.html