Substitute Algorithm

类别:Java 点击:0 评论:0 推荐:
String foundPerson(String[] people){
    for(int i=0;i<people.length;i++){
        if(people[i].equals("Don")){
            return "Don";
        }
        if(people[i].equals("John")){
            return "John";
        }
        if(people[i].equals("Kent")){
            return "Kent";
        }
    }
    return "";
}
Refactor:
String foundPerson(String[] people){
    List candidates=Arrays.asList(new String[]{"Don","John","Kent"});
    for(int i=0;i<people.length;i++)
        if(candidates.contains(people[i]))
            return people[i];
    return "";
}

本文地址:http://com.8s8s.com/it/it11233.htm