i have a a list of Strings such as: ["T Info1 Info2 Info3 Info4", "R Info1 Info2 Info3 Info4"] and so on..
I want to convert this list into a 2 dimensional list to look like: [ ["T","Info1","Info2"], ["R","Info1","Info2"] ]
Hows the proper coding for it?
appreciate any help!
CodePudding user response:
You can stream over the list and split each array into another list:
final var list = List.of("T Info1 Info2 Info3 Info4", "R Info1 Info2 Info3 Info4");
final var list2d = list.stream()
.map(str -> Arrays.asList(str.split(" ")))
.collect(Collectors.toList());
System.out.println(list2d);
CodePudding user response:
Solution that does not use the stream API.
public class Splitter {
public static void main(String[] args) {
String[] source = {"T Info1 Info2 Info3 Info4","R Info1 Info2 Info3 Info4"};
String[][] result = new String[source.length][];
for (int i = 0; i < source.length; i ) {
result[i] = source[i].split(" ");
}
System.out.println(java.util.Arrays.deepToString(result));
}
}
Output is:
[[T, Info1, Info2, Info3, Info4], [R, Info1, Info2, Info3, Info4]]