java - 枚举数组到字符串数组

标签 java arrays string enums

我有一个枚举数组。现在我想将其转换为一个字符串数组,其中包含方法 Enum#name() 返回的枚举的名称。这是我到目前为止所尝试的(枚举称为“Column”。):

String[] stringArray = Arrays.asList(Column.values()).toArray(String[]::new);

我总是收到 ArrayStoreException。我能做什么?

最佳答案

您需要流式传输枚举,以便在创建数组之前首先将枚举映射到字符串:

String[] arrStr = Arrays.stream(FooEnum.values()) // create stream of enum values
        .map(e -> e.toString())  // convert enum stream to String stream
        .toArray(String[]::new); // convert stream to an array

关于java - 枚举数组到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202492/

相关文章:

java - 为什么我得到 "The left-hand side of an assignment must be a variable"?

java - 为 Mac bundle 一个 Java 7 .jar

java - 在 RxJava/RxAndroid 中的可观察对象之间传递响应

c# - 找不到 "IndexOutOfRangeException"的原因

c++ - 字符串s中是整数n吗? 4 在 Ab56c4D 中吗? (C++)

c - 将两个字符串读入 1 个变量

java - CDI PostConstruct 和可变字段

java - 在二维数组中添加元素

javascript - JS - 构造函数中的 JSON/ARRAY 传递变量(自定义库)

Java:为什么String相等性可以用==来证明?