Java学习--数组--判断数组中是否包含某个元素的方法
package zaLearnpackage;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; //检查数组是否包含某个值的方法
public class TestArray {
//使用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);
}
//使用循环判断 (效率最高)
public static boolean useLoop(String[] arr,String targetValue){
for(String s:arr){
if(s.equals(targetValue))
return true;
}
return false;
}
//查找有序数组中是否包含某个值的用法
public static boolean useArraysBinarySearch(String[] arr,String targetValue){
int a=Arrays.binarySearch(arr, targetValue);
if(a>)
return true;
else
return false;
}
//使用ArrayUtils
public static boolean useArrayUtils(String[] arr,String targetValue){
return ArrayUtils.contains(arr,targetValue);
}
public static void main(String[] args) {
String[] arr=new String[]{"CD","BC","EF","DE","AB","JK"};
//use list
long startTime=System.nanoTime();
for(int i=;i<;i++){
useList(arr, "A");
}
long endTime=System.nanoTime();
long duration=endTime-startTime;
System.out.println("useList:"+duration/);
//use set
long startTime2=System.nanoTime();
for(int i=;i<;i++){
useSet(arr, "A");
}
long endTime2=System.nanoTime();
long duration2=endTime2-startTime2;
System.out.println("useSet:"+duration/);
//use loop
long startTime3=System.nanoTime();
for(int i=;i<;i++){
useLoop(arr, "A");
}
long endTime3=System.nanoTime();
long duration3=endTime3-startTime3;
System.out.println("useLoop:"+duration/);
//use Arrays.binarySearch()
long startTime4=System.nanoTime();
for(int i=;i<;i++){
useArraysBinarySearch(arr, "A");
}
long endTime4=System.nanoTime();
long duration4=endTime4-startTime4;
System.out.println("useArraysBinarySearch:"+duration/);
}
}
/*
* 显然,使用一个简单的循环方法比使用任何集合都更加高效。
* 许多开发人员为了方便,都使用第一种方法,但是他的效率也相对较低。
* 因为将数组压入Collection类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。
*/
Java学习--数组--判断数组中是否包含某个元素的方法的更多相关文章
- Python 中如何判断 list 中是否包含某个元素
在python中判断 list 中是否包含某个元素: ——可以通过in和not in关键字来判读 例如: abcList=['a','b','c',1,2,3] if 'a' in abcList: ...
- python判断list中是否包含某个元素
python判断list中是否包含某个元素 theList = ['a','b','c'] if 'a' in theList: print 'a in the list' if 'd' not in ...
- 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。
package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...
- ***用php的strpos() 函数判断字符串中是否包含某字符串的方法
判断某字符串中是否包含某字符串的方法 if(strpos('www.idc-gz.com','idc-gz') !== false){ echo '包含'; }else{ echo '不包含'; } ...
- 如何判断list中是否包含某个元素
在python中可以通过in和not in关键字来判读一个list中是否包含一个元素: str = ['s','i','m','o','n'] if 'e' in str: print("e ...
- python中如何判断list中是否包含某个元素
在python中可以通过in和not in关键字来判读一个list中是否包含一个元素 pythontab = ['p','y','t','h','o','n','t','a','b'] if 't' ...
- python 中如何判断list中是否包含某个元素
在python中可以通过in和not in关键字来判读一个list中是否包含一个元素 theList = ['a','b','c'] if 'a' in theList: print 'a in th ...
- js中判断数组中是否包含某元素的方法
方法一:array.indexOf(item,start):元素在数组中的位置,如果没与搜索到则返回 -1. 参数 描述 item 必须.查找的元素. start 可选的整数参数.规定在数组中开始检索 ...
- js 判断字符串中是否包含某个字符串的方法实例
String对象的方法 方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") ...
随机推荐
- Collections.sort排序
默认是升序,即Collections.sort(list),对list进行升序排列,如果想降序则需要通过compare这些参数来实现了
- 研究大华3G设备接入自主视频开发平台
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fengshuiyue/article/details/24311977 近期项目上须要接入3G摄像头 ...
- TypeScript 版本相关
TypeScript 1.3 元组类型 // Declare a tuple type var x: [string, number]; // 初始化 x = []; // ok // 错误的初始化 ...
- RPC概念及分类【转载】
RPC概念及分类 RPC全称为Remote Procedure Call,翻译过来为“远程过程调用”.目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程通信和相互调用 ...
- sql语句实例练习
1.最晚入职员工查询 select * from employees where hire_date = (select max(hire_date) from employees) 2.倒数第三 ...
- 如何长期试用Beyond Compare 4
打开Beyond Compare 4,发现已经过了试用期 我们可以点击立即购买,购买相关的Beyond Compare 4产品,如果你已经有密钥了,可以选择使用密钥 如果还想继续试用,则找到自 ...
- mongodb url
https://blog.csdn.net/jianlong727/article/details/53484440
- css 小坑
1.display:inline-block 内容上下移动 原因:inline-block 默认对齐方式是底部对齐 方法:加一个 vertical-align:top; 属性 把垂直对齐方式改为顶部
- 利用redis 漏洞入侵挖矿临时解决办法
top 看到一个bashd的进程占据了cpu ps aux |grep bashd cd /tmp 发现ddg.2011 的文件.root dump.rdb 在/root/.ssh 也有奇怪的文件 ...
- Oracle中rownum的基本用法
Oracle中rownum的基本用法 对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且r ...