If I have a 2D string array (or int array), how can take this and create a linked list out of it?The linked list can be a 2D linked list or regular linked list and it doesn't matter if the list is double, single, circular or multi-linked lists.
You can either use two nested for
loops or Java 8 Streams.
For a 2-dimensional array of primitives:
final int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 10 } };
final LinkedList<LinkedList<Integer>> list = Arrays.stream(arr)
.map(x -> Arrays.stream(x).boxed().collect(Collectors.toCollection(LinkedList::new)))
.collect(Collectors.toCollection(LinkedList::new));
System.out.println(list);
For a 2-dimensional array of objects:
final String[][] arr2 = {
{"a","b"},{"c"},{"d","e","f"}
};
final LinkedList<LinkedList<String>> list2 =
Arrays.stream(arr2).map(x -> new LinkedList<>(Arrays.asList(x)))
.collect(Collectors.toCollection(LinkedList::new));
System.out.println(list2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.