方式一:使用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;
}
网友评论