java - 将 ArrayList 中的多个元素放入单个数组?

标签 java arrays sorting arraylist

需要一些帮助来了解如何将 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/

相关文章:

php - mysql,从 'today'开始对枚举中的天进行排序(滚动)

java - Spring Data JPA CDI 集成因 Wildfly Swarm 而失败

c++ - 将数字数组动态设置为数组或容器 C++

java - 创建循环来检查数组 (java)

javascript - 动态更改 Highcharts 中一系列的数据

ruby - 阵列半展平

sorting - Mergesort 没有正确计算 1 的左右尺寸

linux - 如何在 bash 中对多行进行排序?

java - MySQL数据库无法插入数据

Java反射: create new instance of interface give java. lang.ClassCastException