gpt4 book ai didi

java - 如何在java中将字符串从3维数组拆分为新的2维数组

转载 作者:行者123 更新时间:2023-12-01 08:56:05 25 4
gpt4 key购买 nike

我有 3 维数组,我想在 [x][x][0] 处拆分以创建新数组。

static String[][][] flight = {
{
{"MON,TUE,WED,THU,FRI,SAT,SUN","TG2040","06:55","07:55"},
{"MON,TUE,WED,THU,FRI,SAT,SUN","TG2042","10:35","11:35"}
},
{
{"MON,TUE,WED,THU,FRI,SAT,SUN","TG2041","08:25","09:20"},
{"MON,TUE,WED,THU,FRI,SAT,SUN","TG2043","12:05","13:05"}
}

我想创建一个像这样的新的二维数组。

String[][] flight_of_day = {
{"MON","TG2040","06:55","07:55"},
{"TUE","TG2040","06:55","07:55"},
{"WED","TG2040","06:55","07:55"},
.... // Monday To Sunday of TG2040

{"MON","TG2042","10:35","11:35"},
{"TUE","TG2042","10:35","11:35"},
.... // Monday To Sunday of TG2042

{"MON","TG2041","08:25","09:20"},
{"TUE","TG2041","08:25","09:20"},
.... // Monday To Sunday of TG2041

最佳答案

    String[][] flight_of_day =
Stream.of(flight)
.flatMap(Arrays::stream)
.flatMap(
t -> Arrays.stream(t[0].split(","))
.map(day -> {
String[] tmp = Arrays.copyOf(t, t.length);
tmp[0] = day;
return tmp;
})
).toArray(String[][]::new);

关于java - 如何在java中将字符串从3维数组拆分为新的2维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42032719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com