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") ...
随机推荐
- python之计算机硬件基本认知_数据单位_进制间转换_数的原码反码补码
一:计算机硬件基本认知 cpu: 中央处理器. 相当于人的大脑.运算中心,控制中心. 内存: 临时存储数据. 优点:读取速度快,缺点:容量小,造价高,断电即消失. 硬盘: 长期存储数据. ...
- [JAVA]字节数组流
import java.io.*; public class ByteArrayStream { public static void main(String[] args) { byte[] dat ...
- 嵌入式GCC笔记
GNU C Complier 查看 gcc 版本 :gcc -v 文件后缀名 .c的后缀 为C语言的文件源码 gcc -o output gcc -o 输出文件名字 输入文件名 gcc -v -o ...
- delphi 多线程之System.TMonitor (续一)
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- PHP中的反射模拟框架中控制器的调度
<?php class IndexAction { public function index() { echo 'index'; } public function indexBefore() ...
- 浏览器端使用javascript调用腾讯翻译api
最近在学习的小玩意,发现腾讯的文档十分坑爹,里面有很多错误的指示. 不过腾讯的机器翻译还是很牛的,我觉得翻译水准比谷歌好很多. 腾讯的机器翻译貌似在试用阶段,不收费,用QQ或微信登录即可申请使用. 首 ...
- Nginx reverse proxy NSQAdmin
以下配置只针对nsqadmin v1.1.0 (built w/go1.10.3)版本 ## The default server# server { listen 80 defau ...
- openFileOutput和openFileInput还有FileOutStream与openFileOutput
FileOutputStream fos = openFileOutput("",MODE_PRIVATE); fos.write("private".getB ...
- 知识点:Mysql 索引原理完全手册(1)
知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) Mysql-索引原理完全手 ...
- SA vs NSA
5G: What is Standalone (SA) vs Non-Standalone (NSA) Networks? According to the recent 3GPP Release 1 ...