簡體   English   中英

如何將枚舉列表轉換為字符串數組?

[英]How to convert list of enums into array of strings?

我有一系列枚舉,如下所示:

enum CountryEnum {
   MADAGASCAR,
   LESOTHO,
   BAHAMAS,
   TURKEY,
   UAE
}

List<CountryEnum> countryList = new ArrayList<>();
countryList.add(CountryEnum.MADAGASCAR);
countryList.add(CountryEnum.BAHAMAS);

如何將我的countryList轉換為String[]

我以這種方式嘗試 eq :

String[] countries = countryList.toArray(String::new);

但它返回給我ArrayStoreException

謝謝你的幫助!

謝謝你的幫助!

它應該這樣工作:

 String[] strings = countryList.stream()
                    .map(Enum::toString)
                    .toArray(String[]::new);

嘗試這個:

CountryEnum[] arr = countryList.stream().toArray(CountryEnum[] ::new);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM