国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java中高效的判斷數(shù)組中某個(gè)元素是否存在詳解

2019-11-26 13:34:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、檢查數(shù)組是否包含某個(gè)值的方法

使用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);}

使用循環(huán)判斷

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

使用Arrays.binarySearch()

Arrays.binarySearch()方法只能用于有序數(shù)組!!!如果數(shù)組無(wú)序的話得到的結(jié)果就會(huì)很奇怪。

查找有序數(shù)組中是否包含某個(gè)值的用法如下:

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

時(shí)間復(fù)雜度

下面的代碼可以大概的得出各種方法的時(shí)間成本。基本思想就是從數(shù)組中查找某個(gè)值,數(shù)組的大小分別是5、1k、10k。這種方法得到的結(jié)果可能并不精確,但是是最簡(jiǎn)單清晰的方式。

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);}

運(yùn)行結(jié)果:

useList: 13useSet: 72useLoop: 5useArraysBinarySearch: 9

使用一個(gè)長(zhǎng)度為1k的數(shù)組

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

結(jié)果:

useList: 112useSet: 2055useLoop: 99useArrayBinary: 12

使用一個(gè)長(zhǎng)度為10k的數(shù)組

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

結(jié)果:

useList: 1590useSet: 23819useLoop: 1526useArrayBinary: 12

小結(jié)

顯然,使用一個(gè)簡(jiǎn)單的循環(huán)方法比使用任何集合都更加高效。許多開(kāi)發(fā)人員為了方便,都使用第一種方法,但是他的效率也相對(duì)較低。因?yàn)閷?shù)組壓入Collection類(lèi)型中,首先要將數(shù)組元素遍歷一遍,然后再使用集合類(lèi)做其他操作。

如果使用Arrays.binarySearch()方法,數(shù)組必須是已排序的。由于上面的數(shù)組并沒(méi)有進(jìn)行排序,所以該方法不可使用。

實(shí)際上,如果你需要借助數(shù)組或者集合類(lèi)高效地檢查數(shù)組中是否包含特定值,一個(gè)已排序的列表或樹(shù)可以做到時(shí)間復(fù)雜度為O(log(n)),hashset可以達(dá)到O(1)。

使用ArrayUtils

除了以上幾種以外,Apache Commons類(lèi)庫(kù)中還提供了一個(gè)ArrayUtils類(lèi),可以使用其contains方法判斷數(shù)組和值的關(guān)系。

import org.apache.commons.lang3.ArrayUtils;public static boolean useArrayUtils(String[] arr, String targetValue) {  return ArrayUtils.contains(arr,targetValue);}

同樣使用以上幾種長(zhǎng)度的數(shù)組進(jìn)行測(cè)試,得出的結(jié)果是該方法的效率介于使用集合和使用循環(huán)判斷之間(有的時(shí)候結(jié)果甚至比使用循環(huán)要理想)。

useList: 323useSet: 3028useLoop: 141useArrayBinary: 12useArrayUtils: 181-------useList: 3703useSet: 35183useLoop: 3218useArrayBinary: 14useArrayUtils: 3125

其實(shí),如果查看ArrayUtils.contains的源碼可以發(fā)現(xiàn),他判斷一個(gè)元素是否包含在數(shù)組中其實(shí)也是使用循環(huán)判斷的方式。

部分代碼如下:

  if(array == null) {    return -1;  } else {    if(startIndex < 0) {      startIndex = 0;    }    int i;    if(objectToFind == null) {      for(i = startIndex; i < array.length; ++i) {        if(array[i] == null) {          return i;        }      }    } else if(array.getClass().getComponentType().isInstance(objectToFind)) {      for(i = startIndex; i < array.length; ++i) {        if(objectToFind.equals(array[i])) {          return i;        }      }    }    return -1;  }

所以,相比較之下,我更傾向于使用ArrayUtils工具類(lèi)來(lái)進(jìn)行一些合數(shù)祖相關(guān)的操作。畢竟他可以讓我少寫(xiě)很多代碼(因?yàn)樽约簩?xiě)代碼難免有Bug,畢竟apache提供的開(kāi)源工具類(lèi)庫(kù)都是經(jīng)過(guò)無(wú)數(shù)開(kāi)發(fā)者考驗(yàn)過(guò)的),而且,效率上也并不低太多。

總結(jié)

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用Java能有一定的幫助,如果有疑問(wèn)大家可以留言交流。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 太保市| 吴忠市| 兰考县| 东海县| 化德县| 夹江县| 宜兴市| 凌海市| 宜章县| 汉寿县| 盈江县| 陆川县| 当雄县| 建水县| 邵阳县| 青铜峡市| 桓仁| 綦江县| 汤原县| 乌苏市| 治县。| 涿鹿县| 临桂县| 正镶白旗| 股票| 拉孜县| 清水河县| 临澧县| 南部县| 阜平县| 浦北县| 镇宁| 康定县| 钦州市| 义乌市| 安宁市| 武鸣县| 万安县| 柘城县| 九江市| 章丘市|