爪哇 - 枚举与数组字段 [英] Java - Enum with array field
本文介绍了爪哇 - 枚举与数组字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要存储一个列表名称和个人昵称为每名枚举在Java中。昵称的数目将不会发生变化。的目的是为了能够从一个昵称获取全名。目前,我已经实现了这个像这样:
I want to store a list names and individual nicknames for each name as an Enum in Java. The number of nicknames will not vary. The purpose is to be able to get a full name from a nickname. Currently I have implemented this like so:
public enum Names {
ELIZABETH(new String[] {"Liz","Bet"}),
...
;
private String[] nicknames;
private Names(String[] nicknames)
{
this.nicknames = nicknames
}
public Names getNameFromNickname(String nickname) {
//Obvious how this works
}
}
我不喜欢比较有重复新的String [] {...}
,所以我想知道如果任何人都可以提出一个替代方案,更简洁,实施这一方法?
I quite dislike having to repeat new String[] {...}
, so I wondered if anyone could suggest an alternative, more concise, method of implementing this?
干杯,
皮特
推荐答案
VARARG参数:
private Names(String... nicknames) {
现在您可以调用构造函数没有明确创建数组:
Now you can invoke constructor without explicitly creating array:
ELIZABETH("Liz", "Bet", "another name")
详细(见的参数的任意数)
Details (see "Arbitrary Number of Arguments" section)
这篇关于爪哇 - 枚举与数组字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文