我正在尝试在ArrayList
. 为此,我正在创建一个交换对象的新列表,然后用交换的列表完全覆盖旧列表。但是,我无法将旧列表中的对象添加到新列表中。
该程序从文本文件中获取输入,将数据读入对象(圆形和矩形,它们是 的扩展GeometricObject
),然后将这些对象添加到被ArrayList
调用的objectList
.
这是代码:
public static <E extends Comparable<E>> void swapCells
(ArrayList<E> objectList, int left, int right) {
/* The user may enter the two indices, "left,"
* and, "right," in any order which they desire.
* Because of this it will be necessary to determine
* which is larger or "right" index, and which is
* the smaller or "left" index
*/
int temp;
ArrayList<GeometricObject> swappedList = new ArrayList<GeometricObject>();
if (left > right) {
// Exchanges left and right
temp = left;
left = right;
right = temp;
}
for (int i = 0; i < objectList.size(); i++) {
if (i == left) {
swappedList.add(objectList.get(right));
System.out.println( swappedList.get(i).getArea());
} else {
swappedList.add((E) objectList.get(i));
}
}
} // End of swapCells
我收到以下语法错误,不知道该怎么办。
add(GeometricObject)
类型中的方法ArrayList<GeometricObject>
不适用于参数(E)
错误特别是在,swappedList.add(objectList.get(right));
也是wappedList.add((E) objectList.get(i));
。