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

时间:2017-02-03 20:43:44

标签: java arrays split

我有三维数组,我想在[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

2 个答案:

答案 0 :(得分:1)

    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);

答案 1 :(得分:0)

这是如何执行此操作的可迭代示例。

            final List<String[]> tmpList = new ArrayList<>();
    for (String[][] t : flight) {
        if (t == null) {
            continue;
        }
        for (String[] k : t) {
            if (k == null || k.length < 1 || k[0] == null) {
                continue;
            }
            for (String day : k[0].split(",")) {
                String[] tmp = Arrays.copyOf(k, k.length);
                tmp[0] = day;
                tmpList.add(tmp);
            }
        }
    }
    String[][] flight_of_day = tmpList.toArray(new String[tmpList.size()][]);