博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何高效检查一个数组中是否包含某个值?
阅读量:4677 次
发布时间:2019-06-09

本文共 2856 字,大约阅读时间需要 9 分钟。

转载地址:http://www.diguage.com/archives/112.html

1、不同的实现方式

1) 使用List

public static boolean useList(String[] arr, String targetValue) {       return Arrays.asList(arr).contains(targetValue);}
2) 使用
Set

public static boolean useSet(String[] arr, String targetValue) {      Set
set = new HashSet
(Arrays.asList(arr)); return set.contains(targetValue);}
3) 使用循环:

public static boolean useLoop(String[] arr, String targetValue) {    for (String s : arr) {        if (s.equals(targetValue)) {            return true;        }    }    return false;}
4) 使用
Arrays.binarySearch

public static boolean useArraysBinarySearch(String[] arr, String targetValue) {    int a = Arrays.binarySearch(arr, targetValue);    if (a > 0) {        return true;    } else {        return false;    }}

2、时间复杂度

使用如下代码来粗略比较不同实现间的时间复杂度。虽然不是很精确,但是思路确实正确的。我们将看看数组在有5、1k、10k个元素的情况下的不同表现。

public static void main(String[] args) {    String[] arr = new String[]{"CD", "BC", "EF", "DE", "AB"};    // use list    long startTime = System.nanoTime();    for (int i = 0; i < 100000; i++) {        useList(arr, "A");    }    long endTime = System.nanoTime();    long duration = endTime - startTime;    System.out.println("useList:  " + duration / 1000000);    // use set    startTime = System.nanoTime();    for (int i = 0; i < 100000; i++) {        useSet(arr, "A");    }    endTime = System.nanoTime();    duration = endTime - startTime;    System.out.println("useSet:  " + duration / 1000000);    // use loop    startTime = System.nanoTime();    for (int i = 0; i < 100000; i++) {        useLoop(arr, "A");    }    endTime = System.nanoTime();    duration = endTime - startTime;    System.out.println("useLoop:  " + duration / 1000000);    // use Arrays . binarySearch ()    startTime = System.nanoTime();    for (int i = 0; i < 100000; i++) {        useArraysBinarySearch(arr, "A");    }    endTime = System.nanoTime();    duration = endTime - startTime;    System.out.println("useArrayBinary:  " + duration / 1000000);}
结果:

useList:  12useSet:  65useLoop:  2useArrayBinary:  7

使用大一点的数组(1k个元素):

int length = 1000;String[] arr = new String[length];Random s = new Random();for (int i = 0; i < length; i++) {    arr[i] = String.valueOf(s.nextInt());}
结果:
useList:  115useSet:  2010useLoop:  97useArrayBinary:  9
使用更大一点的元素(10k个元素):

int length = 10000;String[] arr = new String[length];Random s = new Random();for (int i = 0; i < length; i++) {    arr[i] = String.valueOf(s.nextInt());}

结果:

useList:  1678useSet:  25609useLoop:  1802useArrayBinary:  10
从上面的结果可以清晰看到,使用简单循环的相比使用其他集合操作更高效。很多很多开发人员使用第一种方法,但是它并不是最高效的。将数组转化成其他的任何集合类型都需要先将所有元素读取到集合类中,才能对这个集合类型做其他的事情。

当使用Arrays.binarySearch()方法时,数组必须是排好序的。如果数组不是排好序的,则不能使用这个方法。

事实上,如果你真的需要高效地检查一个数组或者集合中是否包含一个值,一个排好序的数组或者树可以达到O(log(n))的时间复杂度,HashSet甚至能达到O(1)的时间复杂度。

转载于:https://www.cnblogs.com/archermeng/p/7537229.html

你可能感兴趣的文章
OC-类目延展协议
查看>>
LSTM Accuracy
查看>>
$#,$?,$!等说明
查看>>
IOS应用
查看>>
教你用ps如何将图片、文字做出模糊斑驳的作旧效果
查看>>
推排序
查看>>
SPOOL、SQLLOADER数据导出导入的一点小总结
查看>>
js代码移动Div 移动平台与PC平台
查看>>
java学习——网络编程UDP
查看>>
leetcode 136 Single Number, 260 Single Number III
查看>>
WPF——RenderTransform特效
查看>>
使用框架的——好处
查看>>
如此大量的代码,但每个类里面的代码却不显得特别多,原因。。。。。。。。。。。。...
查看>>
C#特征备忘
查看>>
intelil——快捷键
查看>>
Java 面向对象 之 final 关键字
查看>>
Contact Form 7邮件发送失败的解决办法
查看>>
How to use For loop in CruiseControl.net
查看>>
P1800 software_NOI导刊2010提高(06)
查看>>
Python学习日记(1)使用if __name__ == "main"
查看>>