Groovy之美

names = ["Ted", "Fred", "Jed", "Ned"]
println names
shortNames= names.findAll{ it.size() <= 3 }
println shortNames.size()
shortNames.each{ println it }
 

 

import java.util.List;
import java.util.ArrayList;

class Erase {
    private List filterLongerThan(List strings, int length) {
        List result = new ArrayList();
        for (int i = 0; i < strings.size(); i++) {
            String s = (String) strings.get(i);
            if (s.length() <= length) {
                result.add(s);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List names = new ArrayList();
        names.add("Ted"); names.add("Fred");
        names.add("Jed"); names.add("Ned");
        System.out.println(names);
        Erase e = new Erase();
        List shortNames= e.filterLongerThan(names, 3);
        System.out.println(shortNames.size());
        for (int i= 0; i< shortNames.size(); i++) {
            String s = (String) shortNames.get(i);
            System.out.println(s);
        }
    }
}
 

 

你可能感兴趣的:(java,groovy)