检查数组是否包含某个值的方法

使用List

publicstaticbooleanreturn Arrays.asList(arr).contains(targetValue); }

使用Set

publicstaticsetnew HashSet<String>(Arrays.asList(arr)); returnset.contains(targetValue); }

使用循环判断

publicstaticbooleanforifreturntrue; } returnfalse; }

使用 Arrays.binarySearch()

publicstaticbooleanint a = Arrays.binarySearch(arr, targetValue); if0returntrue; elsereturnfalse; }

时间复杂度

publicstaticvoidnew"CD""BC""EF""DE""AB"}; //use listlong startTime = System.nanoTime(); forint0100000; i++) { useList(arr, ); } long endTime = System.nanoTime(); long duration = endTime - startTime; System.out"useList: "1000000); //use set startTime = System.nanoTime(); forint0100000; i++) { useSet(arr, ); } endTime = System.nanoTime(); duration = endTime - startTime; System.out"useSet: "1000000); //use loop startTime = System.nanoTime(); forint0100000; i++) { useLoop(arr, ); } endTime = System.nanoTime(); duration = endTime - startTime; System.out"useLoop: "1000000); //use Arrays.binarySearch() startTime = System.nanoTime(); forint0100000; i++) { useArraysBinarySearch(arr, ); } endTime = System.nanoTime(); duration = endTime - startTime; System.out"useArrayBinary: "1000000); }

: : : :

使用一个长度为1k的数组

new1000]; Random s = new Random(); forint01000; i++){ arr[i] = String.valueOf(s.nextInt()); }

: : : :

使用一个长度为10k的数组

new10000]; Random s = new Random(); forint010000; i++){ arr[i] = String.valueOf(s.nextInt()); }

: : : :

总结

Arrays.binarySearch()使用 ArrayUtils

ArrayUtilscontainsimport org.apache.commons.lang3.ArrayUtils; publicstaticbooleanreturn ArrayUtils.contains(arr,targetValue); }

时间: 2024-08-25 13:35:20

检查数组是否包含某个值的方法的相关文章

js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法

var  questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf = function (e) { for (var i = 0, j; j = this[i]; i++) { if (j.indexOf(e) != -1) { return i; } } return -1; } if (anSwerIdValue.length < 14) { alert(&quo

JavaScript判断数组是否包含指定元素的方法

本文实例讲述了JavaScript判断数组是否包含指定元素的方法.分享给大家供大家参考.具体如下: 这段代码通过prototype定义了数组方法,这样就可以在任意数组调用contains方法 /** * Array.prototype.[method name] allows you to define/overwrite an objects method * needle is the item you are searching for * this is a special variab

php合并数组并保留键值的方法

php合并数组并保留键值的方法例子1,数组使用字符串键名,相同的键名会被后面的覆盖<pre><?php$arr1 = array('name'=>'fdipzone');$arr2 = array('name'=>'terry'); $result = array_merge($arr1, $arr2); print_r($result);?></pre>输出:<pre>Array( [name] => terry)</pre>

java 检查是否是数组 检查是否是空数组 检查数组是否包含某个元素

/** * Determine whether the given object is an array: * either an Object array or a primitive array. * @param obj the object to check */ public static boolean isArray(Object obj) { return (obj != null && obj.getClass().isArray()); } /** * Determin

判断数组是否包含某值

//方法一: Integer[] arr= {1,2,3}; List<Integer> list = Arrays.asList(arr); if (list.contains(1)&&list.contains(2)){ System.out.println("1,2"); } //方法二: String[] arr = {"a","b","c"}; List<String> lis

node js 判断数组中是否包含某个值

判断数组中是否包含某个值这里有四种方法.用的测试数据: let arr=["a","b","c"]; let arr2={"a":"aaa","b":"bbb","c":"ccc"}; in判断是否在数组的key里in操作符针对的是key,而非value.而对于普通的一维数组来说,key是隐藏的.所以,对于判断某个数组中是否含有

Java 高效检查一个数组中是否包含某个值

如何检查一个数组(未排序)中是否包含某个特定的值?在Java中,这是一个非常有用并又很常用的操作.同时,在StackOverflow中,有时一个得票非常高的问题.在得票比较高的几个回答中,时间复杂度差别也很大. 1.不同的实现方式 使用list 1 public static boolean useList(String[] arr, String targetValue) { 2 return Arrays.asList(arr).contains(targetValue); 3 } 使用se

灵魂拷问:如何检查Java数组中是否包含某个值 ?

摘自:https://www.cnblogs.com/qing-gee/p/12053156.html 在逛 programcreek 的时候,我发现了一些专注细节但价值连城的主题.比如说:如何检查Java数组中是否包含某个值 ?像这类灵魂拷问的主题,非常值得深入地研究一下. 另外,我想要告诉大家的是,作为程序员,我们千万不要轻视这些基础的知识点.因为基础的知识点是各种上层技术共同的基础,只有彻底地掌握了这些基础知识点,才能更好地理解程序的运行原理,做出更优化的产品. 我曾在某个技术论坛上分享过

js中判断数组中是否包含某元素的方法

方法一:array.indexOf(item,start):元素在数组中的位置,如果没与搜索到则返回 -1. 参数 描述 item 必须.查找的元素. start 可选的整数参数.规定在数组中开始检索的位置.它的合法取值是 0 到 stringObject.length - 1. 如省略该参数,则将从字符串的首字符开始检索. 实际用法:if(arr.indexOf(某元素) > -1){//则包含该元素} var fruits = ["Banana", "Orange&