對於同類型和同緯度的數組的字節碼是相同的,如:
int [] a1 = new int []{1,2,3};
int [] a2 = new int [4];
System.out.println(a1.getClass()==a2.getClass());
輸出:true
如何快速地遍歷一個數組呢?
使用數組的一個工具類:Arrays
String [] a4 = new String[]{"a","b","c"};
System.out.println(Arrays.asList(a4));
輸出:
[a, b, c]
但是,如果我們使用Arrays.asList遍歷a1會出現什麼狀況呢?
System.out.println(Arrays.asList(a1));
輸出:
[[I@6c908f05]
因爲參數是a1的元素的int類型,不屬於Object的子類,a1會作爲一個數組整體傳入。不能遍歷。