Java - 带有数组字段的枚举
我想将列表名称和每个名称的单独昵称存储为 Java 中的枚举。昵称的数量不会变化。目的是能够从昵称中得到全名。目前我已经这样实现了:
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
}
}
我非常不喜欢重复 new String[] {...}
,所以我想知道是否有人可以建议一种替代的、更简洁的实现方法?
干杯,
皮特
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
}
}
I quite dislike having to repeat new String[] {...}
, so I wondered if anyone could suggest an alternative, more concise, method of implementing this?
Cheers,
Pete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可变参数:
现在您可以调用构造函数而无需显式创建数组:
详细信息(参见“任意数量的参数”部分)
Vararg parameters:
Now you can invoke constructor without explicitly creating array:
Details (see "Arbitrary Number of Arguments" section)