英文:
How to reverse the contents of an enum element in java
问题
以下是翻译好的内容:
这里是我的枚举结构。
public enum DictionaryEnum {
ARROW(
"Arrow",
new String[] {
"名词"
},
new String[] {
"这里有一个箭头:<IMG> -=>> </IMG>"
}
),
BOOK(
"Book",
new String[]{
"名词",
"名词",
"动词",
"动词"
},
new String[]{
"一套页面。",
"以印刷或电子形式出版的书写作品。",
"安排某人在飞机上有座位。",
"在特定日期安排某事。"
}
);
}
基本上,如果用户输入 "book reverse"。输出应该是单词 "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 [名词] : 一套页面。
Book [名词] : 以印刷或电子形式出版的书写作品。
Book [动词] : 安排某人在飞机上有座位。
Book [动词] : 在特定日期安排某事。
如果输入 "book reverse",输出应为:
Book [动词] : 在特定日期安排某事。
Book [动词] : 安排某人在飞机上有座位。
Book [名词] : 以印刷或电子形式出版的书写作品。
Book [名词] : 一套页面。
英文:
So here is my enum structure.
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."
}
);
Basically if the user types in book reverse. The output should be the definitions of the word book in reverse.
Here is what I have so far:
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);
}
}
But as you can guess, it outputs the reverse of the whole enum structure as opposed to the reverse of the individual term inside of the structure.
For Example: if the user types in "book" then the output should be:
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.
if the type in "book reverse", the output should be
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.
答案1
得分: 0
你首先需要找到选择的DictionaryEnum:
DictionaryEnum de = DictionaryEnum.valueOf(keywords[0].toUpperCase());
随后,您可以使用给定枚举的数据结构来打印您想要的内容。
作为注意:对于诸如字典之类的数据结构,我不建议使用不可修改的数据结构,因为很可能在将来您会想要从其他来源添加条目。
英文:
You first need to find the DictionaryEnum selected:
DictionaryEnum de = DictionaryEnum.valueOf(keywords[0].toUpperCase());
Afterwards you can use the data structure of the given Enum to print out what you want.
As a note: I would not use a unmodifiable data structure for something like a dictionary as it is most likely that in future you want to add entries from other sources.
答案2
得分: 0
由于您发布了一个不完整的枚举,我不得不自行完成缺失的部分。因此请注意,枚举字段名称和方法可能与您的代码中的名称不同:
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;
}
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) {
// ...
}
}
我添加的大部分内容都是简单的getter和setter,以及一个构造函数,应该与您的代码基本相同。可能对您有兴趣的是我添加的静态方法public static DictionaryEnum getByName(String name)
。此方法接受一个字符串参数,然后遍历所有已存在的枚举对象。它将传递的参数与名称字段进行比较,如果相等(忽略大小写),则会返回该枚举对象。如果循环在没有找到匹配项的情况下完成运行,它将返回null。
因此,像这样调用该方法:DictionaryEnum.getByName("arrow");
将会返回对象 DictionaryEnum.ARROW
。您可以通过这个找到的枚举对象正常或反向迭代数组字段。您可以在我添加的主方法中看到所有这些,并运行代码本身。
英文:
Since you posted an incomplete enum I had to complete the missing parts myself. So keep in mind that the enum field names and methods might have a different name than in your code:
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();
}
}
Now most of what i added are simple getters/setters and a constructor that should be more or less identical to what you have. What is probably interesting for you is the static method public static DictionaryEnum getByName(String name)
that I added. This method takes an String argument and then iterates over all the enum object that exists. It compares the passed argument with the name field and if it is equal (ignoring case) it will return that enum object. If the loop finished runnig without ever finding a match it will return null instead.
So calling that method like DictionaryEnum.getByName("arrow");
will return you the object DictionaryEnum.ARROW
.
There are many different ways to find the enum you want by an input (See the answer of Fabian Tschachtli for another example), but you need to get the correct enum object first before you can do anything else.
All you then have to do is use that found enum object to iterate over the Array fields either normally or in reverse. You can see all that in the main method I added and run the code itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论