3 回答

TA貢獻1775條經驗 獲得超11個贊
如果UserType
是一個enum
,你可以用靜態UserType.valueOf()
方法轉換它,即:
UserType userType = UserType.valueOf(Line[5]);
現在您可以在以下代碼中使用userType
來代替。Line[5]
請注意,Line[5]
必須與任何枚舉類型的拼寫完全匹配,否則IllegalArgumentException
將拋出異常。

TA貢獻1876條經驗 獲得超6個贊
如果UserType.valueOf無法提供您需要的內容,請添加返回特定實例的靜態方法UserType。例如:-
public enum UserType{
CUSTOMER("customer");
private String name;
UserType(String name){
this.name = name;
}
//A Map that holds user type name as key.
private static Map<String,UserType> userTypeMap = new HashMap<>();
//Populate userTypeMap with UserType instance
static{
for(UserType type : values()){
userTypeMap.put(type.name, type);
}
}
public static UserType of(String name){
return userTypeMap.get(name);
}
}
調用者可以獲得UserType如下實例:-
UserType type = UserType.of("customer");

TA貢獻1797條經驗 獲得超6個贊
你的解決方案太長了。有一種優雅的方法可以做到這一點
Properties properties = new Properties();
properties.load(new FileInputStream(new File("FileLocation")));
properties.values();
文件可以包含值列表、鍵值對。在第 2 行,Properties 對象中加載了第 2 個文件?,F在您可以根據您的需要進行處理。
添加回答
舉報