簡體   English   中英

嘗試獲取Java函數以基於傳遞給它的字符串參數返回數組

[英]Trying to get a Java function to return an array based on a string parameter passed to it

這就是我想要做的; 我正在嘗試編寫一個小程序,該程序使用一個函數來獲取一個包含基於所選部門的部門成員的數組。 大約有40個部門,每個部門有1-4個成員。 所以我做了這個,但是我似乎無法解析為一個變量:

從主要:

String[]names = initial(department)

部門是傳遞給功能的部門的名稱。 在函數中,我有:

public static String[] initial (String responsibleDepartment) {

    if (responsibleDepartment.equals("Accounts Receivable")) {
        String[] assignInitial = {"Mike Davis", "Ben Jones", "Ann Smith"}
     }

    if (responsibleDepartment.equals("Customer Service")) {
        String[] assignInitial = {"Mary Wexler", "Turd Ferguson"}
     }

   // and 38 more if statements with the rest of the departments. Finally I have:
        return assignInitial;
        }

如此看來,“ assignInitial無法解析為變量”(這就是return語句的意思。)如果我嘗試在第一個“ if”語句之前初始化變量,然后刪除String []的初始化在第一個“ if”語句中的assignInitial旁邊,然后顯示“數組常量只能在初始化程序中使用”。 我相信有更好的方法可以做到這一點,但我似乎無法在網上找到任何進展。 有沒有人?

非常感謝! -蒙克

問題在於您要在if語句中聲明每個String[] assignInitial ,因此該變量的作用域是if語句的局部范圍。 if語句之外,未聲明該變量。

要解決此問題,請將聲明從if范圍內拉入方法范圍。 您必須在每個數組前面添加new String[] ,因為數組常量只能在初始化程序中使用。

public static String[] initial (String responsibleDepartment) {

String[] assignInitial = null;

if (responsibleDepartment.equals("Accounts Receivable")) {
    // Note the new String[] below
    assignInitial = new String[] {"Mike Davis", "Ben Jones", "Ann Smith"};
 }

if (responsibleDepartment.equals("Customer Service")) {
    assignInitial = new String[] {"Mary Wexler", "Turd Ferguson"};
 }

// Other if statements here

return assignInitial;

請注意,除了這一系列的if語句之外,還有其他更簡便的方法來查找assignInitial的值。 例如,您可以構建一個switch語句或Map<String, String>

由於尚無人提出建議,因此Java支持帶字符串的開關,因此不需要一堆if語句。 您可以使用一個開關,如果有那么多的if語句,它會稍微快一些。

switch(responsibleDepartment) {

  case "Accounts Receivable":
    return new String[]{"Mike Davis", "Ben Jones", "Ann Smith"};
  ...

或者,您也可以創建地圖並構建地圖,然后像這樣訪問地圖

map.get(responsibleDepartment) // Returns {"Mike Davis", "Ben Jones", "Ann Smith"}

如果您需要並發使用ConcurrentHashMap

您需要創建變量,然后將值分配為在if條件之外可訪問的值。 或者,如果您不需要在函數中執行任何其他操作,則可以在創建變量后立即返回該變量以停止方法的流程。

public static String[] initial (String responsibleDepartment) {
    if (responsibleDepartment.equals("Accounts Receivable")) {
        String[] assignInitial = {"Mike Davis", "Ben Jones", "Ann Smith"};
        return assignInitial;
    }  

    if (responsibleDepartment.equals("Customer Service")) {
        String[] assignInitial = {"Mary Wexler", "Turd Ferguson"}
        return assignInitial;
    }
    // and 38 more if statements with the rest of the departments. Finally I have:
    return null;
    }

如果在if上下文中定義變量,則在上下文完成后將無法返回該變量。

嘗試此操作,初始指定外部條件,並在所有位置使用變量。

public static String[] initial (String responsibleDepartment) {
    String[] assignInitial = null;

    if (responsibleDepartment.equals("Accounts Receivable")) {
        assignInitial = {"Mike Davis", "Ben Jones", "Ann Smith"}
     }

    if (responsibleDepartment.equals("Customer Service")) {
        assignInitial = {"Mary Wexler", "Turd Ferguson"}
     }

   // and 38 more if statements with the rest of the departments. Finally I have:
        return assignInitial;
        }

您也可以用這種方法來完成,並跳過整個變量。 如果不匹配,它將返回一個空數組。

public static String[] initial (String responsibleDepartment) {

    if (responsibleDepartment.equals("Accounts Receivable")) {
        return new String[] {"Mike Davis", "Ben Jones", "Ann Smith"};
     }

    if (responsibleDepartment.equals("Customer Service")) {
         return new String[] {"Mary Wexler", "Turd Ferguson"};
     }

    return new String[] {};

}

我建議您進行一下更改,以使代碼更具可讀性,並且不會濫用返回值:

public static String[] initial (String responsibleDepartment) {
    String[] names = null;

    if (responsibleDepartment.equals("Accounts Receivable")) {
        names = new String[] {"Mike Davis", "Ben Jones", "Ann Smith"};
     }

    if (responsibleDepartment.equals("Customer Service")) {
        names = new String[] {"Mary Wexler", "Turd Ferguson"};
     }

    return names;
}

您的代碼有兩個問題:

(1) 變量assignInitial范圍限制在if塊內,因此不能在if塊外使用它

(2)另外,如下所示使用else if代替許多if條件(如果不使用else if ,則將評估所有其他38個if條件,這是不必要的)

public static String[] initial (String responsibleDepartment) {
    String[] assignInitial = null;
    if (responsibleDepartment.equals("Accounts Receivable")) {
        assignInitial = new String[] {"Mike Davis", "Ben Jones", "Ann Smith"};
    } else if (responsibleDepartment.equals("Customer Service")) {
        assignInitial = new String[] {"Mary Wexler", "Turd Ferguson"};
    } // and 38 more if statements with the rest of the departments.
   return assignInitial;
}

我認為您在這里采用了錯誤的方法。

從根本上說,您的整個方法都執行與Map相同的操作:“給定 (字符串)輸入,它映射到什么結果(字符串數組)輸出?”

因此,作為對現有代碼的精確重寫,我將首先執行以下操作:

private static final Map<String, String[]> INITIALS_BY_DEPARTMENT;

static {
    INITIALS_BY_DEPARTMENT = new HashMap<>();
    INITIALS_BY_DEPARTMENT.put("Accounts Receivable", new String[]{"Mike Davis", "Ben Jones", "Ann Smith"});
    INITIALS_BY_DEPARTMENT.put("Customer Service", new String[]{"Mary Wexler", "Turd Ferguson"});
    // other assignments
}

public static String[] initial (String responsibleDepartment) {
    return INITIALS_BY_DEPARTMENT.get(responsibleDepartment);
}

這段代碼仍然散發出難聞的氣味,那就是您的邏輯中混合了數據 理想情況下,將有一些外部數據庫或其他配置來定義哪些人負責每個部門。 您無需在代碼中定義一個常量映射,而可以在啟動時將其讀入該映射。 這樣,您就不必對值進行硬編碼,也不會冒與現實不合時宜的風險。

有時當然這是不可能的,並且這些信息純屬您的應用程序內部。 但是對於此處暗示的用例,幾乎可以肯定地在某些外部系統中定義了從部門到用戶的映射。 即使您現在無法從中讀取內容,也請至少將映射分離到配置文件中,並讓加載程序讀取該內容。 這樣,您已將數據與邏輯分離,並且將來可以輕松切換為從其他來源讀取數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM