【发布时间】:2018-10-11 10:07:49
【问题描述】:
我有以下代码:
public static void poistaKaikki32(LinkedList L1, Collection L2) {
LinkedList<Integer> temp = new LinkedList<>();
HashSet<Integer> L2Set = new HashSet<>(L2);
// first filter elements into temp
while (L1.size() > 0) { // n loops
int v = L1.removeFirst(); <--- getting error cannot convert object to int
if (!L2Set.contains(v)) {
temp.addLast(v);
}
}
// add filtered values back to L1
while (temp.size() > 0) {
L1.addLast(temp.removeFirst());
}
}
我不断收到关于 int v = L1.removeFirst(); 的错误。如果不使用演员表,我将如何解决这个问题。
【问题讨论】:
-
removeFirst()的返回类型是什么? -
如果参数
L1是一个包含Integers的LinkedList,则声明为LinkedList<Integer> L1;如果由于对调用代码的负面影响而无法执行此操作,则必须使用(Integer)演员表。 -
还有:了解 java 命名约定。变量/参数名称采用驼峰命名法。并且:使用有意义的来告诉人类读者发生了什么。 L1 没有任何意义,什么也说不出来。
-
是的,@luk2302,演员表不是最佳的;但是如果有很多东西用原始的
Lists 调用这个东西怎么办?
标签: java methods collections linked-list