int[] ints = {1,2,3,4};
if(Arrays.asList(ints).contains(3)){
System.out.println("true");
}else{
System.out.println("false");
}
輸出false
從我理解角度講講這種現象
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
1、基本數據類型是無法泛型化的,也就是說 8 個基本類型是無法作爲 asList 的參數的,爲啥沒報錯呢,因爲數組是對象。當成對象了。輸出ints可見 [I@1ebdff3b
int[] ints = {1,2,3,4};
if(Arrays.asList(ints).contains(ints)){
System.out.println("true");
}else{
System.out.println("false");
}
輸出true
2、修改int[] 成 Integer[] ,再次contains(3),true。
asList 方法。Java將可變長參數當成數組對待。可以將一個數組或可變的參數個數傳遞給可變長參數。當用可變的參數個數調用方法時,Java會創建一個數組並把參數傳給它。
a本質就是一個數組,所以當對象類型數組直接可以傳給它