在Java中使用switch块而不是多个if语句

在Java中使用switch块而不是多个if语句

public class A {
  public void search(boolean[] searchList) {
    // searchList array is used to identify what options to search for in a given order
   // e.g. boolean [] searchList = new boolean [] {false, false, true, false};
    boolean searchL = false;
    boolean searchM = false;
    boolean searchK = false;
    boolean searchA = false;

    if(searchList[0] == true) searchL = true;
    if(searchList[1] == true) searchM = true;
    if(searchList[2] == true) searchK = true;
    if(searchList[3] == true) searchA = true;

    if(searchL == true) // write a query to search for all Ls
    if(searchM == true) // write a query to search for all Ms
    ...........
}


有没有一种方法可以简化此代码?

@All:很抱歉之前发布了一个错误的问题。我很困惑!

谢谢,
了索尼

最佳答案

public class A {

    public enum SearchOption {
        SEARCH_L,
        SEARCH_M,
        SEARCH_A,
        SEARCH_K;
    }
    /**
     * Make them pass in an enum for your search.
     * Pros: type safe, can only use the selections you give
     * Cons: must add to the enum to add new types
     * @param option
     */
    public void enumSearch(SearchOption option) {

        switch(option) {
        case SEARCH_A:
            System.out.println("I am searching for A");
            break;
        case SEARCH_K:
            System.out.println("I am searching for K");
            break;
        case SEARCH_L:
            System.out.println("I am searching for L");
            break;
        case SEARCH_M:
            System.out.println("I am searching for M");
            break;
        }
    }

    /**
     * Use a primitive for your input
     * Pros: Gives you more options without updating the enum
     * Cons: Users could enter input you don't really want them to use
     * @param option
     */
    public void charSearch(char option) {
        switch(option) {
        case 'a':
        case 'A':
            System.out.println("I am searching for A");
            break;
        case 'k':
        case 'K':
            System.out.println("I am searching for K");
            break;
        case 'l':
        case 'L':
            System.out.println("I am searching for L");
            break;
        case 'm':
        case 'M':
            System.out.println("I am searching for M");
            break;
        }
    }

    /**
     * Use a primitive and don't even actually check it! Just run with it!
     * @param option
     */
    public void uncheckedSearch(char option) {
        System.out.println("I am searching for " + option);
    }
}


根据您的评论,这是我对该方法的更新示例-确保顶部的评论已更新!

/**
 * Perform the search based on the options provided
 * The list should be in the order of L, M, A, K
 * @note update this comment as more search options are added
 * @param searchList the list of flags indicating what to search for
 */
public void search(boolean[] searchList) {

    // as per docs, [0] denotes an L search:
    if(searchList[0])
        // write a query to search for all Ls

    // as per docs, [1] denotes an M search:
    if(searchList[1])
        // write a query to search for all Ms

    // as per docs, [2] denotes an A search:
    if(searchList[2])
        // write a query to search for all As

    // as per docs, [3] denotes a K search:
    if(searchList[3])
        // write a query to search for all Ks
}


最新想法:

// Use the SearchOption enum from above
Map<SearchOption, String> searches = new HashMap<SearchOption, String>();

public List<SearchResult> search(List<SearchOption> options) {
    List<SearchResult> results = new LinkedList<SearchResult>();
    for(SearchOption option : options) {
        String query = searches.get(option);
        SearchResult result = MySearchService.executeQuery(query);
        results.add(result);
    }
    return results;

}

关于java - 在Java中使用switch块而不是多个if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5043295/

10-10 11:13