方法一 采用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
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)*/
ListlistDemo2 =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 */