static void filterData(ArrayList<Data> dataList, Predicate<Data> p){
Iterator<Data> i = dataList.iterator();
while (i.hasNext()){
if (p.test(i.next())){
i.remove();
}
}
}
...........
ArrayList<Data> al = new ArrayList<>();
Data d;
d = new Data(1); al.add(d);
d = new Data(2); al.add(d);
d = new Data(3); al.add(d);
//INSERT METHOD CALL HERE
System.out.println(al);
As the problem statement says, these are just code fragments. Not complete code listings. You need to assume that they are present within valid context where they will compile and run.
No, filterData need not be static. The given code fragment could also be a part of some other instance method:
class Test{
void filterData(...){ ... }
void someOtherMethod(){
ArrayList<Data> al = new ArrayList<Data>(); //not shown in the given code, assume that it exists
Data d = new Data(1); al.add(d);
d = new Data(2); al.add(d);
d = new Data(3); al.add(d);
//INSERT METHOD CALL HERE
System.out.println(al);
}
}
otherwise filterData method seems to be declared static ?
comparing with the other questions this seems very ambiguous.
No, you don't need to do d.filterData because someOtherMethod and filterData methods are instance methods of the same class Test in the code that I showed above. Implicit variable this will be used.
I understand that you feel it is weird but you will see partial code fragments in the exam also. You should assume that they are placed in valid context.