我一直在嘗試訪問數組列表中保存的幾個數組的元素。我可以定期訪問它,但是當我使用泛型類型E來說明不同的數據類型時,問題就出現了。這給了我一個類演員異常。如果我將tempStart和tempScan的類型以及相應的強制類型轉換爲int [](因爲這是我用來傳入的),它會運行。如何訪問陣列的通用數組列表中的元素
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
if (list.get(0).getClass().isArray()) {
System.out.println(" I am an array!");
//go through the arrays and make sure they are
//not the same, remove any that are the same
//make flag to see if something is different
boolean matching;
for (int idx = 0; idx < list.size() - 1; idx++) {
E[] tempStart =(E[])list.get(idx);
for (int k = idx + 1; k < list.size(); k++) {
matching = true;
E[] tempScan = (E[])list.get(k);
for (int index = 0; index < tempStart.length; index++) {
if (tempStart[index] != tempScan[index]) {
matching = false;
}
}
if (matching) {
list.remove(tempScan);
k--;
}
}
}
您的for循環遍歷list.size。你是否正在檢查數組中的兩個數組是否相同,或者數組中的元素是否相同? – lmcphers