如题所述
在Java中,检测一个数组是否包含某一个数据,通常有四种方法:
(1)for循环
(2)转换为List,调用Arrays.asList(arr).contains方法
(3)使用Set
(4)使用Arrays.binarySearch()方法
下面为上述四种方法的具体代码实现:
1、使用for循环
publicstaticbooleanuseLoop(String[]arr,StringtargetValue){
for(Strings:arr){
if(s.equals(targetValue))
returntrue;
}
returnfalse;
}
/iknow-pic.cdn.bcebos.com/d62a6059252dd42a816ca8e00e3b5bb5c9eab810"target="_blank"title="点击查看大图"class="ikqb_img_alink">/iknow-pic.cdn.bcebos.com/d62a6059252dd42a816ca8e00e3b5bb5c9eab810?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="https://iknow-pic.cdn.bcebos.com/d62a6059252dd42a816ca8e00e3b5bb5c9eab810"/>
2、转换为List,调用Arrays.asList(arr).contains方法
publicstaticbooleanuseList(String[]arr,StringtargetValue){
returnArrays.asList(arr).contains(targetValue);
}
/iknow-pic.cdn.bcebos.com/8cb1cb1349540923258eca359f58d109b2de4992"target="_blank"title="点击查看大图"class="ikqb_img_alink">/iknow-pic.cdn.bcebos.com/8cb1cb1349540923258eca359f58d109b2de4992?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="https://iknow-pic.cdn.bcebos.com/8cb1cb1349540923258eca359f58d109b2de4992"/>
3、使用Set
publicstaticbooleanuseSet(String[]arr,StringtargetValue){
SetStringset=newHashSetString(Arrays.asList(arr));
returnset.contains(targetValue);
}
/iknow-pic.cdn.bcebos.com/e61190ef76c6a7ef2dea016df0faaf51f2de66a9"target="_blank"title="点击查看大图"class="ikqb_img_alink">/iknow-pic.cdn.bcebos.com/e61190ef76c6a7ef2dea016df0faaf51f2de66a9?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="https://iknow-pic.cdn.bcebos.com/e61190ef76c6a7ef2dea016df0faaf51f2de66a9"/>
4、使用Arrays.binarySearch()方法
特别说明:binarySearch()二分查找仅适用于有序数组,如果不是有序数组,则报异常
publicstaticbooleanuseArraysBinarySearch(String[]arr,StringtargetValue){
inta=Arrays.binarySearch(arr,targetValue);
if(a0){
returntrue;
}else{
returnfalse;
}}
/iknow-pic.cdn.bcebos.com/8ad4b31c8701a18bec0bbb04932f07082938fe9d"target="_blank"title="点击查看大图"class="ikqb_img_alink">/iknow-pic.cdn.bcebos.com/8ad4b31c8701a18bec0bbb04932f07082938fe9d?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="https://iknow-pic.cdn.bcebos.com/8ad4b31c8701a18bec0bbb04932f07082938fe9d"/>
扩展资料:
Java种List列表的contains方法:
该方法是通过遍历集合中的每一个元素并用equals方法比较是否存在指定的元素。
publicbooleancontains(Objecto){
IteratorEit=iterator();
if(o==null){
while(it.hasNext())
if(it.next()==null)
returntrue;
}else{
while(it.hasNext())
if(o.equals(it.next()))
returntrue;
}
returnfalse;
}
/iknow-pic.cdn.bcebos.com/64380cd7912397dd61e8334f5482b2b7d1a287ec"target="_blank"title="点击查看大图"class="ikqb_img_alink">/iknow-pic.cdn.bcebos.com/64380cd7912397dd61e8334f5482b2b7d1a287ec?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto"esrc="https://iknow-pic.cdn.bcebos.com/64380cd7912397dd61e8334f5482b2b7d1a287ec"/>
参考资料来源:/docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html"target="_blank"title="只支持选中一个链接时生效">Java官网-API-Arrays
参考资料来源:/docs.oracle.com/javase/7/docs/api/java/util/List.html"target="_blank"title="只支持选中一个链接时生效">Java官网-API-InterfaceList