previous | index | next

Programming with Iterators

A StringFilter object can be passed to a method that uses it to filter longer strings from a list of strings, as in the following test class:
public class FilterTester
{
    public static void main(String[] args)
    {
        ArrayList<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("six");
        list.add("seven");
        ArrayList<String> newList = filter(list, new StringFilter());
        System.out.println(newList); // should display [one, two, six]
    }

    public static ArrayList<String> filter(ArrayList<String> a, Filter f)
    {
        ...
    }
}

Complete the filter method using a list iterator.


previous | index | next