我一直在尝试访问数组列表中保存的几个数组的元素。我可以定期访问它,但是当我使用泛型类型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