在 Dartlang 中旋转/移动列表?

5 dart

Dart 中是否有更好/更快的方法来旋转列表?

List<Object> rotate(List<Object> l, int i) {
  i = i % l.length;

  List<Object> x = l.sublist(i);
  x.addAll(l.sublist(0, i));

  return x;
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 5

可以简化一点

List<Object> rotate(List<Object> list, int v) {
  if(list == null || list.isEmpty) return list;
  var i = v % list.length;
  return list.sublist(i)..addAll(list.sublist(0, i));
}
Run Code Online (Sandbox Code Playgroud)