短:
这可能有一个不是 Object
的子类的类吗? ?
长:
我正在使用 List<T>
其中包含接口(interface)的实现(Operation
):
public class OperationQueue<T extends Operation> {
private final List<T> mQueue = new ArrayList<>();
...
}
当我在循环中遍历这个列表时,我将每个完成的操作标记为空,因为我必须保持相同的大小并且我不能使用迭代器(它抛出 ConcurrentModificationException
):
for (int i = 0; i < mQueue.size() && !mState.equals(State.PAUSED); i++) {
mOperation = mQueue.get(i);
if (mOperation != null) {
mOperation.run();
}
mQueue.set(i, null);
}
当我暂停或完成我使用的队列执行时:
mQueue.removeAll(Collections.singleton(null));
Android Studio 向我显示警告:
'List<T>' may not contain type objects of type 'Object'
这怎么可能?我以为一切都是Object
的 child ?
如有任何帮助,我们将不胜感激。
最佳答案
一切都是 Object
的 child : 但您的 List 需要它们是 Object 的特定类型的子对象,即:T
类型.
Collections.singleton(null)
不是 T 的集合。尝试 Collections.<T>singleton(null)
;通过将说明符添加到方法调用中,您可以返回指定类型的集合。
关于java - '列表<T >' may not contain type objects of type ' 对象',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255779/