如何在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."
                }
        );

基本上,如果用户以反向方式输入书籍。输出应该是单词本的相反定义。

这是我到目前为止所拥有的:

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反转”,则输出应为

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:


const button = Vue.component('button',{

props: {
    id: String,color: String
},data: function() {

    return {

        count: 0
    }

},template: 

`
  <div class="button">
     {{ count }}
  </div>
`
});

然后,您可以使用给定Enum的数据结构来打印所需的内容。

请注意:我不会对字典等内容使用不可修改的数据结构,因为将来很可能要添加其他来源的条目。

,

由于您发布的枚举不完整,所以我必须自己填写缺失的部分。因此请记住,枚举字段名称和方法的名称可能与代码中的名称不同:

lambdaNumbertOneFunctionName - value-1
lambdaNumberOneFunctionArn - value-1
lambdaNumberTwoFunctionName - value-2
lambdaNumberTwoFunctionArn - value-2

现在,我添加的大多数内容都是简单的getter / setter和构造函数,该构造函数应与您所拥有的大致相同。您可能感兴趣的是我添加的静态方法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(); } } 。此方法采用String参数,然后遍历存在的所有枚举对象。它将传递的参数与名称字段进行比较,如果相等(忽略大小写),它将返回该枚举对象。如果循环在未找到匹配项的情况下结束了runnig,它将返回null。

因此,调用类似public static DictionaryEnum getByName(String name)的方法将返回对象DictionaryEnum.getByName("arrow");

有很多方法可以通过输入查找所需的枚举(有关另一个示例,请参见Fabian Tschachtli的答案),但是您需要首先获取正确的枚举对象,然后才能执行其他操作。

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