flutter 在Dart中将1d列表转换为2d列表

1dkrff03  于 2022-12-30  发布在  Flutter
关注(0)|答案(2)|浏览(160)

我有一个1d的整数列表,如:

List<int> x = [1, 4, 2, 8, 9, 3, 6, 5, 7];

我想把这个列表转换成一个二维列表,如下所示:

List<List<int>> y = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
r55awzrz

r55awzrz1#

List<int> x = [1, 4, 2, 8, 9, 3, 6, 5, 7];

// make ascending list 

List<int> x = (b, a) => a.compareTo(b);

// install package matrix 2d dart package link : //https://pub.dev/packages/matrix2d

list = X.reshape(3,3);
6uxekuva

6uxekuva2#

List<dynamic> oneDimensionalList = [1, 2, 3, 4, 5];

List<List<dynamic>> twoDimensionalList = oneDimensionalList.map((element) => [element]).toList();

print(twoDimensionalList);  // Output: [[1], [2], [3], [4], [5]]

相关问题