e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

示例:

package test3;

import java.util.Arrays;
import java.util.List;

public class Test5 {
    
    /* 原函数 */
    public 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 "";
    }

    /* 修改后 */
    public String foundPerson2(String[] people){
        List<String> 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 "";
    }
    
}

 

posted on 2017-06-11 13:55  纯黑Se丶  阅读(131)  评论(0编辑  收藏  举报