gpt4 book ai didi

Java 枚举数组(类)

转载 作者:行者123 更新时间:2023-12-01 18:32:38 25 4
gpt4 key购买 nike

做这样的事情有多可行?考虑 Enum* 一个 enum Enum* {/* ... */}...

public static void main(String[] args) {
?[] types = new ?[] { Enum1, Enum2, Enum3 };

for(? type : types) {
for(Enum elem : type.values() { /* ... */ }
}
}

我的主要要求基本上是一堆字符串常量,每个常量都以不同的类型分组。我是否可以更好地设置二维字符串数组(String[][])?我想要一些更受约束的值,而不是 types[j],其中 j==1A,而 j==2B

我知道我可以尝试一下反射,但我敢说它会变得太复杂,而且可读性较差。

最佳答案

也许这个结构可以解决您的问题:

    enum SomeEnum {
A("one", "two", "three"),
B("hello", "there");

public final String[] constants;

SomeEnum(String... constants) {
this.constants = constants;
}
}

关于Java 枚举数组(类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23457966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com