如何判断数组中包含某一个值

2025-05-04 13:18:09

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢