JAVA - 最合适的数据结构

Ion*_*dan 5 java data-structures

我是Java的新手,但现在我面临两难选择.我有一个错误列表,看起来像:

"ERROR CODE" "POSITION" "Error description"
"000" "1" "No error"
"001" "1" "Connection error"
"002" "1" "Error sending reversal or batch capture process"
"003" "1" "Error after authorization – message sent to host and answer received"
"004" "1" "Error sending message for authorization"
"005" "1" "Error receiving message from host"
Run Code Online (Sandbox Code Playgroud)

还有很多错误.

现在我正在开发一个JAVA库,我真正需要做的是实现这些错误(错误永远不会改变,它们总是相同的)以某种方式使得使用该库的开发人员可以通过给定的ERROR_CODE轻松识别错误描述.

例如:String getError(ERROR_CODE); 并返回与ERROR_CODE关联的错误描述的字符串.

我想要声明ENUM数据结构,但我似乎无法使其正常工作.

非常感谢你.

mun*_*ngm 3

您可以像这样使用枚举:

\n\n
enum Error {\n\n    ERROR_000("000", 1, "No error"),\n    ERROR_001("001", 1, "Connection error"),\n    ERROR_002("002", 1, "Error sending reversal or batch capture process"),\n    ERROR_003("003", 1, "Error after authorization \xe2\x80\x93 message sent" +\n                        "to host and answer received"),\n    ERROR_004("004", 1, "Error sending message for authorization"),\n    ERROR_005("005", 1, "Error receiving message from host");\n\n    private final String code;\n    private final int position;\n    private final String description;\n    private static final Map<String, Error> errorMap =\n        new HashMap<String, Error>();\n\n    static {\n        for (Error error : Error.values()) {\n            errorMap.put(error.code, error);\n        }\n    }\n\n    Error(final String code, final int position, final String description) {\n        this.code = code;\n        this.position = position;\n        this.description = description;\n    }\n\n    public static Error getError(String code) {\n        return errorMap.get(code);\n    }\n    // add getters and setters here:\n    public String getCode() { return this.code; }\n    public int getPosition() { return this.position; }\n    public String getDescription() { return this.description; }\n}\n
Run Code Online (Sandbox Code Playgroud)\n