removeRange method

  1. @override
void removeRange(
  1. int start,
  2. int end
)
inherited

Removes a range of elements from the list.

Removes the elements with positions greater than or equal to start and less than end, from the list. This reduces the list's length by end - start.

The provided range, given by start and end, must be valid. A range from start to end is valid if 0 ≤ startendlength. An empty range (with end == start) is valid.

The list must be growable.

final numbers = <int>[1, 2, 3, 4, 5];
numbers.removeRange(1, 4);
print(numbers); // [1, 5]

Implementation

@override
void removeRange(int start, int end) {
  var length = this.length;
  RangeError.checkValidRange(start, end, length);

  // Special-case removing an initial or final range because we can do it very
  // efficiently by adjusting `_head` or `_tail`.
  if (start == 0) {
    _head = (_head + end) & (_table.length - 1);
    return;
  }

  var elementsAfter = length - end;
  if (elementsAfter == 0) {
    _tail = (_head + start) & (_table.length - 1);
    return;
  }

  // Choose whether to copy from the beginning of the end of the queue based
  // on which will require fewer copied elements.
  var removedElements = end - start;
  if (start < elementsAfter) {
    setRange(removedElements, end, this);
    _head = (_head + removedElements) & (_table.length - 1);
  } else {
    setRange(start, length - removedElements, this, end);
    _tail = (_tail - removedElements) & (_table.length - 1);
  }
}