replaceRange():
Replaces a range of elements with the elements of
replacements
.Removes the objects in the range from
start
toend
, then inserts the elements ofreplacements
atstart
.The provided range, given by
start
andend
, must be valid.
Example:
extension IterableExtension<E> on Iterable<E> {
Iterable<E> replaceRange(int start, int end, Iterable<E> newElements) sync* {
yield* this.take(start);
yield* newElements;
yield* this.skip(end);
}
}
void main() {
List<int> myList = [1, 2, 3, 4, 5];
List<int> newElements = [6, 7, 8];
myList.replaceRange(1, 3, newElements);
print(myList); // Output: [1, 6, 7, 8, 4, 5]
}
[1, 6, 7, 8, 4, 5]
Exited.