zoukankan      html  css  js  c++  java
  • 检查数组是否包含某个值的方法

    检查数组是否包含某个值的方法
    使用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); }
  • 相关阅读:
    pandas 生成excel
    身份证校验规则
    css 模态框
    python3 打开MySQL时:RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods 报错
    selenium元素定位
    LR的基本知识
    python3的编码报错解决办法
    MySQL的简单条件判断语句
    Java判断一个字符串中包含另一字符串
    使用线程池获取执行结果,CountDownLatch+ThreadPool,FutureTask+ThreadPool 并比较
  • 原文地址:https://www.cnblogs.com/luxd/p/5976761.html
Copyright © 2011-2022 走看看