What is a good switch statement alternative?

后端 未结 4 838
时光取名叫无心
时光取名叫无心 2021-01-28 16:48

I have a string array containing strings of 3 letters each. Every 3 letters (every element) corresponds to a unique letter. I need to create a char array from the string array.<

4条回答
  •  遥遥无期
    2021-01-28 17:38

    You could use an Enum to define your elements and the corresponding character. Then have a method that decrypts the value.

    Example:

    public enum EncryptedValue {
    
        A ("A", "EDK"),
        B ("B", "CHI"),
        C ("C", "WAD"),
        ...;
    
        private String value;
        private String encryption;
    
        private static final List VALUES = Collections.unmodifiableList(Arrays.asList(values()));
    
        private EncryptedValue(String value, String encryption) {
            this.value = value;
            this.encryption = encryption;
        }
    
        public String getValue() {
            return value;
        }
    
        public String getEncryption() {
            return encryption;
        }
    
        public String decrypt(Strign encryption) {
            for (EncryptedValue encryptedValue : VALUES) {
                if (encryptedValue.getEncryption().equalsIgnoreCase(encyrption))
                    return encryptedValue.getValue();
            }
    
            return null;
        }
    
    }
    

提交回复
热议问题