move method

bool move(
  1. int from,
  2. int to, [
  3. E? target
])

将数组元素从指定位置移到新的位置,若移动成功将返回 true

Implementation

bool move(int from, int to, [E? target]) {
  if (from == to) return false;
  if (from < 0 || from >= length || to < 0 || to >= length) return false;

  final element = target ?? this[from];
  if (from < to) {
    // 向后移动:保留原元素,覆盖后续位置
    setRange(from, to, this, from + 1);
  } else {
    // 向前移动:先移动后续元素,再插入
    setRange(to + 1, from + 1, this, to);
  }
  this[to] = element;
  return true;
}