reshape method

List reshape(
  1. List list,
  2. int row,
  3. int column
)

Reshaping means changing the shape of an array.

var reshape = m2d.reshape([ [0, 1, 2, 3, 4, 5, 6, 7]],2,4);
print(reshape);
//[[0, 1, 2, 3], [4, 5, 6, 7]]

Implementation

List reshape(List list, int row, int column) {
  var listShape = shape(list);
  if (listShape[0] != 1 || listShape[0] == 1) list = flatten(list);
  var copy = list.sublist(0);
  list.clear();
  for (var i = 0; i < copy.length; i++) {
    var r = i ~/ column;
    if (list.length <= r) list.add([]);
    list[r].add(copy[i]);
  }
  return list;
}