如何在另一个枚举中格式化ZodiacInfo中的枚举常量的名称



我的程序有一个包含构造函数的枚举。

public enum ZodiacType {
ARI(ZodiacInfo.Aries, "March 21", "April 19"),
TAU(ZodiacInfo.Taurus, "April 20", "May 20"),
GEM(ZodiacInfo.Gemini, "May 21", "June 20"),
CAN(ZodiacInfo.Cancer, "June 21", "July 22"),
LEO(ZodiacInfo.Leo, "July 23", "August 22"),
VIR(ZodiacInfo.Virgo, "August 23", "September 22"),
LIB(ZodiacInfo.Libra, "September 23", "October 22"),
SCO(ZodiacInfo.Scorpio, "October 23", "November 21"),
SAG(ZodiacInfo.Sagittarius, "November 22", "December 21"),
CAP(ZodiacInfo.Capricorn, "December 22", "January 19"),
AQU(ZodiacInfo.Aquarius, "January 20", "February 18"),
PIS(ZodiacInfo.Pisces, "February 19", "March 20");
private final ZodiacInfo description;
private final String startDate;
private final String toDate;
ZodiacType(ZodiacInfo description, String startDate, String toDate) {
this.description = description;
this.startDate = startDate;
this.toDate = toDate;
}
}

这是我目前的产出,感谢过去的解决方案。

下面是一个集合的例子:

A = {LEO, CAN, VIR, GEM, SAG, TAU, CAP}
// All elements in set are distinct and random number

我的自定义类Set:

public class Set {
private ArrayList<ZodiacType> s;
public Set() {
s = new ArrayList<>();
}
@Override
public String toString() {
return String.format("A = {%s}",
s.toString().replace("[", "").replace("]", ""));
}
}

这是我开发的方法。

public String getStringZodiacInfoFormat() {
// not correct to put arrayList
ArrayList<String> strings = new ArrayList<>();
for (ZodiacType zodiacType: s) {
strings.add(zodiacType.name());
}
return String.format("A = {%s}",
strings.toString().replace("[", "").replace("]", ""))
}

问题是我无法转换它,结果是:

Notation in enum format
A = {GEM, LIB, CAN, PIS, SCO, ARI, VIR}
......................................................

取而代之的是:

Notation in enum format
A = {Gemini, Libra, Cancer, Pisces, Scorpio, Aries, Virgo}
......................................................

我可以从任何给定的解决方案中学到新东西。谢谢

您可以使用StringJoiner来实现所需的字符串表示。

其中一个构造函数允许指定分隔符,以及连接字符串中使用的前缀后缀

方法StringJoiner.add()用于向StringJoiner添加一个新字符串。方法StringJoiner.toString()生成结果字符串。

假设成员ZodiacInfo具有常规的大写名称(ARIES、VIRGO等(,则可以使用以下代码将大写描述连接在一起:

public String getStringZodiacInfoFormat() {

StringJoiner strings = new StringJoiner(", ", "{", "}");

for (ZodiacType zodiacType: s) {

String type = zodiacType.description().name();
strings.add(type.charAt(0) + type.substring(1).toLowerCase());
}
return strings.toString();
}

注意:更干净的方法是将创建大写ZodiacInfo名称的逻辑放入ZodiacType枚举的toString()实现中,或者如果您也想在toString()中显示日期,则引入一个单独的方法,比如getZodiacName()

这就是这种方法的样子:

public enum ZodiacType {
// enum-members, etc.

public String getZodiacName() {
String type = description.name();
return type.charAt(0) + type.substring(1).toLowerCase();
}
}

根据您的代码判断,ZodiacInfo成员的名称与Java命名约定不一致(即ZodiacInfo.Aries而不是推荐的ZodiacInfo.ARIES(。

如果出于某种原因,您想保留它们(AriesVirgo(,那么您不需要将名称大写的逻辑。

方法getStringZodiacInfoFormat()可以这样写:

public String getStringZodiacInfoFormat() {

StringJoiner strings = new StringJoiner(", ", "{", "}");

for (ZodiacType zodiacType: s) {

strings.add(zodiacType.description().name());
}
return strings.toString();
}

最新更新