package cn.edut.test_list;
import java.util.*;
public class Demo_removeEvensVer1 {
public static void main(String[] args) {
Integer[] a = {6,5,1,4,2};
System.out.println(removeEvensVer1(createArrayList(a)).toString());
try {
System.out.println(removeEvensVer2(createArrayList(a)).toString());
}catch (Exception e) {
System.out.println("算法二:迭代器非法");
}
System.out.println(removeEvensVer3(createArrayList(a)).toString());
}
public static <T> List<T> createArrayList(T[] a) {
List<T> lst = new LinkedList<>();
for(int i=0 ; i<a.length ;i++) {
lst.add(a[i]);
}
return lst;
}
public static <T extends Integer > List<T> removeEvensVer1(List<T> lst) {
int i=0 ;
while( i < lst.size()) {
if(lst.get(i)%2==0) {
lst.remove(i);
}else {
i++;
}
}
return lst;
}
public static <T extends Integer > List<T> removeEvensVer2(List<T> lst) {
for(T i : lst) {
if(i%2==0) {
lst.remove(i) ;
}
}
return lst ;
}
public static <T extends Integer > List<T> removeEvensVer3(List<T> lst) {
Iterator<T> iterator = lst.iterator() ;
while(iterator.hasNext()) {
T temp = iterator.next();
if(temp%2==0) {
iterator.remove();
}
}
return lst;
}
}