0

I have 3-d Array and I want to split at [x][x][0] to make new array.

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"}
      }

I want to make a new 2-d array like this.

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
3
  • 2
    Instead of using arrays (of arrays)*, define classes and use instances. Commented Feb 3, 2017 at 20:47
  • Is the 3-D Array an input you have no control over? Because like the other commenter says, classes and instances seems like a more elegant way. Commented Feb 3, 2017 at 21:40
  • i did't understand please example and explain. Commented Feb 5, 2017 at 17:13

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

yup you are faster then me +1
Thank you very much. but are you have way to use for-loop?
0

This is iterable example how to do this.

            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()][]);

3 Comments

Thank you very much
have error at this statement for(String[][] t:flight){ if(t == null){ continue; }
@PhijakChanyawiwatkul I don't see any problem in this code unless the flight is null then the method should return / throws exception: if(flight == null){ return; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.