如何判断数组中包含某一个值
1、四种不同方式检查数组是否包含某个值使用List:public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); }

3、使用简单的循环语句:public static boolean useLoop(String[] arr, String targetValue) { for (String s : arr) { if (s.equals(targetValue)) return true; } return false; }

5、运行下面程序,你有可能会得到异常结果;public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays.binarySearch(arr, targetValue); if (a > 0) return true; else return false; }

7、数组大小为1000:String[] arr = new String[1000]; Random s = new Random(); for (int i = 0; i < 1000; i++) { arr[i] = String.valueOf(s.nextInt()); } 运行结果:useList: 112 useSet: 2055 useLoop: 99 useArrayBinary: 12

9、以上就是小编带给大家的如何判断数组中包含某一个值的关键所在,希望大家可以喜欢,如果喜欢的话可以点赞哦,也可以发表自己的看法