java 将嵌套列表转换为二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26726366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:29:47  来源:igfitidea点击:

Convert nested list to 2d array

javamultidimensional-arraynested-lists

提问by Ypnypn

I'm trying to convert a nested list into a 2d array.

我正在尝试将嵌套列表转换为二维数组。

List<List<String>> list = new ArrayList<>();

list.add(Arrays.asList("a", "b", "c"));
list.add(Arrays.asList("dd"));
list.add(Arrays.asList("eee", "fff"));

I want to make this a String[][]. I've tried the following:

我想把它变成一个String[][]. 我尝试了以下方法:

String[][] array = (String[][]) list.toArray();      // ClassCastException

String[][] array = list.toArray(new String[3][3]);   // ArrayStoreException

String[][] array = (String[][]) list.stream()        // ClassCastException
    .map(sublist -> (String[]) sublist.toArray()).toArray();

Is there a way that works? Note that I won't know the size of the list until runtime, and it may be jagged.

有没有有效的方法?请注意,直到运行时我才会知道列表的大小,并且它可能是锯齿状的。

采纳答案by Pshemo

There is no simple builtin way to do what you want because your toArraycan return only array of elements stored in list which in your case would also be lists.

没有简单的内置方法可以做您想做的事情,因为您toArray只能返回存储在列表中的元素数组,在您的情况下也将是列表。

Simplest solution would be creating two dimensional array and filling it with results of toArrayfrom each of nested lists.

最简单的解决方案是创建二维数组并用toArray每个嵌套列表的结果填充它。

String[][] array = new String[list.size()][];

int i = 0;
for (List<String> nestedList : list) {
    array[i++] = nestedList.toArray(new String[nestedList.size()]);
}

(you can shorten this code if you are using Java 8 with streams just like Alex did)

(如果您像Alex 一样使用带有流的 Java 8,则可以缩短此代码)

回答by Alex

You could do this:

你可以这样做:

String[][] array = list.stream()
    .map(l -> l.stream().toArray(String[]::new))
    .toArray(String[][]::new);

It creates a Stream<List<String>>from your list of lists, then from that uses mapto replace each of the lists with an array of strings which results in a Stream<String[]>, then finally calls toArray(with a generator function, instead of the no-parameter version) on that to produce the String[][].

Stream<List<String>>从您的列表列表中创建 a ,然后使用它map用一个字符串数组替换每个列表,这导致 a Stream<String[]>,然后最后调用toArray(使用生成器函数,而不是无参数版本)以生成的String[][]

回答by Alican OZER

This is the best and most efficient way to convert 2d list to 2d array;

这是将二维列表转换为二维数组的最佳和最有效的方法;

List<List<Integer>>  list2d = new ArrayList<>();
Integer[][] array2d;

array2d = list2d.stream().map(x->x.toArray(new Integer[x.size()])).toArray(Integer[][]::new);

回答by Bryan Correll

Slightly briefer than Alex's answer:

比亚历克斯的回答略简短:

final List<List<String>> list = ...;
final String[][] array = list.stream().map(List::toArray).toArray(String[][]::new);