如何在java中反转枚举元素的内容
这是我的枚举结构如何在java中反转枚举元素的内容,java,data-structures,enumerator,Java,Data Structures,Enumerator,这是我的枚举结构 public enum DictionaryEnum { ARROW( "Arrow", new String[] { "noun" }, new String[] { "Here is on
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."
}
);
但正如您所猜测的,它输出整个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."
}
);
例如:如果用户在“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());
之后,可以使用给定枚举的数据结构打印出所需内容
请注意:我不会对字典之类的东西使用不可修改的数据结构,因为将来很可能需要添加来自其他来源的条目。您首先需要找到DictionaryEnum selected:
DictionaryEnum de = DictionaryEnum.valueOf(keywords[0].toUpperCase());
之后,可以使用给定枚举的数据结构打印出所需内容
请注意:我不会对字典之类的东西使用不可修改的数据结构,因为将来您很可能希望添加其他来源的条目。由于您发布了一个不完整的枚举,我必须自己完成缺少的部分。因此请记住,枚举字段名称和方法可能与代码中的名称不同:
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();
}
}
公共枚举字典yenum{
箭(
“箭”,
新字符串[]{
“名词”
},
新字符串[]{
“这里有一个箭头:-=>>”
}
),
书(
“书”,
新字符串[]{
“名词”,
“名词”,
“动词”,
“动词”
},
新字符串[]{
“一套书页。”,
“以印刷或电子形式出版的书面作品。”,
“安排某人坐飞机。”,
“在特定日期安排某事。”
}
);
私有最终字符串名;
私有最终字符串[]密钥;
私有最终字符串[]值;
private DictionaryEnum(字符串名称、字符串[]键、字符串[]值){
this.name=名称;
this.keys=keys;
这个值=值;
}
公共字符串getName(){
返回名称;
}
公共字符串[]getKeys(){
返回键;
}
公共字符串[]getValues(){
返回值;
}
/**
*将返回传递名称的fitting DictionaryEnum,如果找不到,则返回null
*@param name
*@返回
*/
公共静态字典num getByName(字符串名称){
对于(DictionaryEnum DictionaryEnum:values()){
if(dictionaryEnum.getName().equalsIgnoreCase(名称)){
返回字典yenum;
}
}
返回null;
}
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入命令:”);
字符串输入=scanner.nextLine();
字符串[]关键字=输入。拆分(“”);
字符串名称=关键字[0];
字符串命令=关键字[1];
//1.使用我们的静态方法获取拟合枚举:
DictionaryEnum DictionaryEnum=DictionaryEnum.getByName(名称);
如果(dictionaryEnum==null){
//如果返回的对象为null,则找不到拟合匹配项
System.out.println(“未找到枚举!”);
}否则{
//2.我们找到了一个合适的枚举对象!让我们使用该对象对其内容进行迭代
如果(“反向”。等于(命令)){
对于(int i=dictionaryEnum.getKeys().length-1;i>=0;i--){
String key=dictionaryEnum.getKeys()[i];
字符串值=dictionaryEnum.getValues()[i];
System.out.println(dictionaryEnum.getName()+“[”+键+“]:“+值);
}
}否则{
for(int i=0;i
现在,我添加的大部分内容都是简单的getter/setter和构造函数,它们应该或多或少与您所拥有的内容相同。您可能感兴趣的是我添加的静态方法publicstaticdictionaryenumgetbyname(stringname)
。此方法接受一个字符串参数,然后遍历所有存在的枚举对象。它将传递的参数与name字段进行比较,如果相等(忽略大小写),它将返回该枚举对象。如果循环在没有找到匹配项的情况下完成运行,它将返回null
所以调用该方法,比如DictionaryEnum.getByName(“箭头”)
将返回对象DictionaryEnum.ARROW
有许多不同的方法可以通过输入找到所需的枚举(另一个示例请参见Fabian Tschachtli的答案),但在执行其他操作之前,需要先获得正确的枚举对象
然后,您所要做的就是使用找到的枚举对象在数组字段上正常或反向迭代。您可以在我添加的主方法中看到所有这些,并运行代码本身。由于您发布了一个不完整的枚举,我不得不自己完成缺少的部分。因此请记住,枚举字段名称和方法可能与代码中的名称不同:
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();
}
}
公共枚举字典yenum{
箭(
“箭”,
新字符串[]{
“名词”
},
新字符串[]{
“这里有一个箭头:-=>>”
}