当前位置 : 主页 > 编程语言 > c++ >

java 判断字符串是否在该集合当中,除了遍历外其他方法,其他类型好像也有效

来源:互联网 收集:自由互联 发布时间:2021-07-03
方法一 采用contains 函数 List listDemo2 =new ArrayList (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.contains(key)){ System.out.println("contains true "+listD
方法一 采用contains 函数
List
 
   listDemo2 =new ArrayList
  
   (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.contains(key)){ System.out.println("contains true "+listDemo2.contains(key)); }else{ System.out.println("contains flase "+listDemo2.contains(key)); } /**contains boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). Specified by:contains in interface Collection
   
    Parameters:o - element whose presence in this list is to be testedReturns:true if this list contains the specified elementThrows:ClassCastException - if the type of the specified element is incompatible with this list (optional)NullPointerException - if the specified element is null and this list does not permit null elements (optional)*/
   
  
 
方法二 采用获取下标的方法
List
 
   listDemo2 =new ArrayList
  
   (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.indexOf(key) >= 0){ System.out.println("indexOf true "+listDemo2.indexOf(key)); }else{ System.out.println("indexOf flase "+listDemo2.indexOf(key)); } /* 如果存在,下标就为0或正值,反之为-1 */
  
 
网友评论