如何反转 java 中枚举元素的内容

How to reverse the contents of an enum element in java

这是我的枚举结构。

public enum DictionaryEnum {
        ARROW(
                "Arrow",
                new String[] {
                        "noun"
                },
                new String[] {
                        "Here is one arrow: <IMG> -=>> </IMG>"
                }
        ),
        BOOK(
                "Book",
                new String[]{
                        "noun",
                        "noun",
                        "verb",
                        "verb"
                },
                new String[]{
                        "A set of pages.",
                        "A written work published in printed or electronic form.",
                        "To arrange for someone to have a seat on a plane.",
                        "To arrange something on a particular date."
                }
        );

基本上如果用户输入倒书。输出的应该是word book的反义词定义。

这是我目前的情况:

String[] keywords = input.split(" ");
            String key = keywords[0];
            String[][] result = dictionary.get(key);

    if (keywords[1].toLowerCase().equals("reverse") && !keywords[1].toLowerCase().equals("distinct")) {
                        for (int i = DictionaryEnum.values().length - 1; i >= 0; --i) {
                            DictionaryEnum e = DictionaryEnum.values()[i];
                            System.out.println(e);
                        }
                    }

但是您可以猜到,它输出整个枚举结构的反转,而不是结构内部单个项的反转。

例如:如果用户输入“book”,那么输出应该是:

Book [noun] : A set of pages.
Book [noun] : A written work published in printed or electronic form.
Book [verb] : To arrange for someone to have a seat on a plane.
Book [verb] : To arrange something on a particular date.

如果输入“book reverse”,输出应该是

Book [verb] : To arrange something on a particular date.
Book [verb] : To arrange for someone to have a seat on a plane.
Book [noun] : A written work published in printed or electronic form.
Book [noun] : A set of pages.

您首先需要找到所选的 DictionaryEnum:

  DictionaryEnum de = DictionaryEnum.valueOf(keywords[0].toUpperCase());

之后就可以使用给定的Enum的数据结构打印出你想要的了。

注意:我不会将不可修改的数据结构用于字典之类的东西,因为将来您很可能想从其他来源添加条目。

由于您发布了一个不完整的枚举,我不得不自己完成缺失的部分。因此请记住,枚举字段名称和方法的名称可能与您的代码中的名称不同:

public enum DictionaryEnum {
        ARROW(
                "Arrow",
                new String[] {
                        "noun"
                },
                new String[] {
                        "Here is one arrow: <IMG> -=>> </IMG>"
                }
        ),
        BOOK(
                "Book",
                new String[]{
                        "noun",
                        "noun",
                        "verb",
                        "verb"
                },
                new String[]{
                        "A set of pages.",
                        "A written work published in printed or electronic form.",
                        "To arrange for someone to have a seat on a plane.",
                        "To arrange something on a particular date."
                }
        );
    
    private final String name;
    private final String[] keys;
    private final String[] values;
    
    
    private DictionaryEnum(String name, String[] keys, String[] values) {
        this.name = name;
        this.keys = keys;
        this.values = values;
    }
    
    public String getName() {
        return name;
    }


    public String[] getKeys() {
        return keys;
    }


    public String[] getValues() {
        return values;
    }
    
    /**
     * Will return the fitting DictionaryEnum for the passed name, or null if not found
     * @param name
     * @return
     */
    public static DictionaryEnum getByName(String name) {
        for (DictionaryEnum dictionaryEnum : values()) {
            if(dictionaryEnum.getName().equalsIgnoreCase(name)) {
                return dictionaryEnum;
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter command:");
        String input = scanner.nextLine();
        String[] keywords = input.split(" ");
        String name = keywords[0];
        String command = keywords[1];
        
        // 1. Use our static method to get the fitting enum:
        DictionaryEnum dictionaryEnum = DictionaryEnum.getByName(name);
        if (dictionaryEnum == null) {
            // if the returned object was null no fitting match was found
            System.out.println("NO ENUM FOUND!");
        } else {
            // 2. We found a fitting enum object! Lets use that object to iterate over its contents
            if ("reverse".equals(command)) {
                for (int i = dictionaryEnum.getKeys().length - 1; i >= 0; i--) {
                    String key = dictionaryEnum.getKeys()[i];
                    String value = dictionaryEnum.getValues()[i];
                    System.out.println(dictionaryEnum.getName() + " [" + key + "] : " + value);
                }
            } else {
                for (int i = 0; i < dictionaryEnum.getKeys().length; i++) {
                    String key = dictionaryEnum.getKeys()[i];
                    String value = dictionaryEnum.getValues()[i];
                    System.out.println(dictionaryEnum.getName() + " [" + key + "] : " + value);
                }
            }
        }
        
        scanner.close();
    }
}

现在我添加的大部分内容都是简单的 getters/setters 和一个应该与您所拥有的大致相同的构造函数。您可能感兴趣的是我添加的静态方法 public static DictionaryEnum getByName(String name)。此方法采用 String 参数,然后遍历所有存在的枚举对象。它将传递的参数与名称字段进行比较,如果相等(忽略大小写),它将 return 该枚举对象。如果循环结束 运行nig 而没有找到匹配项,它将 return null 相反。

所以像 DictionaryEnum.getByName("arrow"); 这样调用该方法将 return 你对象 DictionaryEnum.ARROW.

有很多不同的方法可以通过输入找到你想要的枚举(另一个例子参见 Fabian Tschachtli 的回答),但你需要先获得正确的枚举对象,然后才能做任何其他事情。

然后您所要做的就是使用找到的枚举对象正常或反向迭代数组字段。您可以在我添加的主要方法和 运行 代码本身中看到所有内容。