一:运用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; }
四:运用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推断数组是不是包括某个元素的细致内容,更多请关注ki4网别的相干文章!