此代码有问题。它引发“对于类型Main类型,方法collectFrom(List
“ test1”方法中的选择器应从小于10的列表号中进行选择,而映射器应将所选数字增加10。
import java.util.*;
public class Main {
public Main() {
List<Integer> src1 = Arrays.asList(1, 7, 9, 11, 12);
System.out.println(test1(src1));
}
public List<Integer> test1(List<Integer> src) {
Selector<Integer> sel = new Selector<Integer>() {
@Override
public boolean select(Integer a) {
if(a < 10) {
return true;
} else {
return false;
}
}
};
Mapper<Integer> map = new Mapper<Integer>() {
@Override
public Integer map(Integer a) {
return a+=10;
}
};
return collectFrom(src).when(sel).mapEvery(map);
}
public static void main(String[] args) {
new Main();
}
}
这是我的课程ListCreator:
import java.util.ArrayList;
import java.util.List;
public class ListCreator <T>{
List<T> lista;
private ListCreator(List<T> src) {
this.lista = src;
}
public static <T> ListCreator<T> collectFrom(List<T> src) {
ListCreator<T> ls = new ListCreator<T>(src);
return ls;
};
public ListCreator<T> when(Selector s) {
List<T> whenLista = new ArrayList<T>();
for(int i = 0; i < lista.size(); ++i) {
if(s.select(lista.get(i))) {
whenLista.add(lista.get(i));
}
}
this.lista = whenLista;
return this;
};
public List<Integer> mapEvery (Mapper m) {
List<Integer> mapLista = new ArrayList<Integer>();
for(int i = 0; i < lista.size(); ++i) {
mapLista.add((Integer)m.map(lista.get(i)));
}
return mapLista;
}
}
选择器和映射器只是简单的参数化接口。
答案 0 :(得分:0)
collectFrom is a static method defined in the
ListCreator class, not in
主要`。您可以使通话合格:
return ListCreator.collectFrom(src).when(sel).mapEvery(map);
或者只是静态导入它:
import static mypackage.ListCreator.collectFrom;