需要一些帮助来了解如何将 ArrayList 中的所有元素放入单个数组中。不确定是否可以在单个数组中完成它。
声明
List componentNameList = new ArrayList();
String[] componentNameItem = soapApiCall.getComponentNames();
componentNameList.add(Arrays.toString(componentNameItem));
这是 ArrayList 的元素:
[[Index, Pattern, Smart, Intell][Index, Tree, Pet, Intel][Index, Pattern, Bear, Intell, Dog][Sky, Intern, Blond]]
数组的预期输出
<Index><Pattern><Smart><Intell><Index><Tree><Pet><Intel><Index><Pattern><Bear><Intell><Dog><Sky><Intern><Blond>
提前致谢。
最佳答案
首先,我建议您不要使用原始类型,也不建议将数组的字符串表示形式添加到原始类型列表中。
因此,更改此:
List componentNameList = new ArrayList();
对此:
List<List<String>> componentNameList = new ArrayList<>();
然后更改此:
componentNameList.add(Arrays.toString(componentNameItem));
对此:
componentNameList.add(new ArrayList<>(Arrays.asList(componentNameItem)));
然后您可以使用如下所示的流来完成手头的任务:
String[] resultSet = componentNameList.stream()
.flatMap(List::stream) // flatten
.toArray(String[]::new); // collect to array
然后打印:
System.out.println(Arrays.toString(resultSet));
关于java - 将 ArrayList 中的多个元素放入单个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051687/