将2d char数组转换为1d字符串数组

时间:2018-01-31 19:13:19

标签: java java-8

如何使用stream?

对于Ex:

{{1}}

为:

{{1}}

2 个答案:

答案 0 :(得分:6)

代码解释了自己。但请在下次尝试尝试

char[][] ch = new char[3][];
ch[0] = new char[]{'a', 'b', 'c'};
ch[1] = new char[]{'d', 'e', 'f'};
ch[2] = new char[]{'g', 'h', 'i'};

String[] arr = Arrays.stream(ch)
        .map(String::new)
        .toArray(String[]::new);

答案 1 :(得分:0)

您也可以使用Stream.of()选项而不是Arrays.stream

char[][] ch = new char[3][];
ch[0] = new char[]{'a', 'b', 'c'};
ch[1] = new char[]{'d', 'e', 'f'};
ch[2] = new char[]{'g', 'h', 'i'};

String[] s = Stream.of(ch)
       .map(String::new)
       .toArray(String[]::new);

或者,如果您想将其转换为List<String>

List<String> list = Stream.of(ch)
        .map(String::new)
        .collect(Collectors.toList());