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);
方法一 采用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.contains(key)) System.out.println(contains true);
方法一 采用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)*/
方法二 采用获取下标的方法