简体   繁体   English

如何转换清单 <object> 到字符串数组

[英]How to convert list<object> to String Array

How can I convert a object list to a string array? 如何将对象列表转换为字符串数组?

I tried the following code, but I get an ArrayStoreException Error 我尝试了以下代码,但出现ArrayStoreException错误

 List<Object> content = mdp.getContent(); //From docx4j API
 Object[] objectArray = content.toArray();
 String[] paragraphs = Arrays.copyOf(objectArray, objectArray.length, String[].class); 

Thanks 谢谢

If you already have just strings in your list, then it's a one-step operation: 如果列表中仅包含字符串,那么这是一步操作:

content.toArray(new String[content.size()]);

Otherwise you'll have to iterate over each member, applying toString() or whichever method you must use to get the string representation of each list member. 否则,您将必须遍历每个成员,应用toString()或必须使用的任何方法来获取每个列表成员的字符串表示形式。 For that case there is no shortcut in the JDK. 在这种情况下,JDK中没有快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM