Java泛型-clearList实现

Java泛型-clearList实现,java,generics,Java,Generics,我正在尝试使用泛型编写一个简单的方法。但我得到了一个错误的说法 类型迭代器中的方法remove()不适用于参数(T)“ 这是代码 <T> void empty(List<T> list){ Iterator<T> itr = list.iterator(); while(itr.hasNext()){ itr.remove(itr.next());//Error here } } void empty(列

我正在尝试使用泛型编写一个简单的方法。但我得到了一个错误的说法

类型
迭代器中的方法remove()不适用于参数
(T)

这是代码

<T> void empty(List<T> list){       
    Iterator<T> itr = list.iterator();
    while(itr.hasNext()){
        itr.remove(itr.next());//Error here
    }
}
void empty(列表){
迭代器itr=list.Iterator();
while(itr.hasNext()){
itr.remove(itr.next());//此处出错
}
}
未收到任何参数。请从方法中删除该参数

//advance the iterator
itr.next();
//remove the element
itr.remove();
如果要删除列表中的特定元素,请先与所需条件进行比较,然后删除该元素:

while(itr.hasNext()) {
    T foo = itr.next();
    //do the comparison
    if ( ... ) { // using foo
        itr.remove();
    }
}
Iterator.remove()
不接受任何参数

public interface Iterator<E>    
从基础集合中删除此迭代器返回的最后一个元素(可选操作)。每次调用next()时只能调用一次此方法。如果在迭代过程中以除调用此方法以外的任何方式修改基础集合,则迭代器的行为未指定


Oops!。难怪谷歌搜索没有给我答案。谢谢。
Iterator
只有
remove()
,没有
remove()
void remove():