-3

How might I convert List<List<String>> to String[][]?

I've played around with the solutions provided here but not having much luck.

Any help would be appreciated.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Rusty Shackleford
  • 337
  • 1
  • 6
  • 18
  • Happy to edit but not sure what more i can add besides my initial fumbled attempts. Likewise why is the question in the below link open when it's a very similar question (in terms of structure) https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java – Rusty Shackleford Feb 16 '21 at 00:09
  • The reason why it is still open is because it was the first question about this, commonly asked problem. Individually seen, it is badly asked as well (no details, no attempts, ...). First questions to common problems are typically accepted (especially if there are good answers) in order to enrich SOs Q&A repository. – Zabuzard Feb 16 '21 at 00:34
  • That said, I guess the primary reason for the downvotes and close is because you do not show your attempts nor explain what exactly is wrong with them or unclear to you. You do show a research attempt but you do not explain why it did not solve your problem. – Zabuzard Feb 16 '21 at 00:36
  • 1
    Ok that's fair enough, I'll keep that in mind going forwards - Thanks for the answer anyways – Rusty Shackleford Feb 16 '21 at 00:37
  • 1
    Your welcome. I think this question has high potential, it is definitely a frequently searched topic. If you [edit] it and provide all the details that would make it a question of outstanding quality, I am sure people would be happy to reopen it and give upvotes. – Zabuzard Feb 16 '21 at 00:39
  • Nah that's fine, I'm not too bothered about votes. Will just leave up here as an example of a poorly asked question – Rusty Shackleford Feb 16 '21 at 00:40

1 Answers1

3

Explanation

You can not just apply outer.toArray(...) because that would give you an array of lists, List<String>[].

You have to first convert all the inner lists before you can collect them into your resulting data structure. There is no utility method available that can do this automatically for you, but it is fairly easy to do it yourself.

Stream API

Streams are a very good candidate for this. Just stream the outer list, convert the inner lists and then use toArray as provided by Stream.

String[][] result = outer.stream()              // Stream<List<String>>
    .map(inner -> inner.toArray(String[]::new)) // Stream<String[]>
    .toArray(String[][]::new);

Traditional loop

You can follow the exact same idea also with a more traditional manual approach where you setup your target outer array, loop over the inner lists, convert them and collect them into your array.

String[][] result = new String[outer.size()];
int i = 0;
for (List<String> inner : outer) {
    result[i] = inner.toArray(String[]::new);
    i++;
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 2
    Your answer looks OK in the code, but the statement _"You have to first convert the inner list and then the outer"_ can be misleading to less-experienced users. In both cases your outer loop iterates the outer collection first and the inner collection***s*** for each outer collection member. – Jim Garrison Feb 15 '21 at 23:56