Метод setRange
Записывает некоторые элементы iterable в диапазон этого списка.
Копирует объекты iterable, пропуская skipCount объектов сначала, в диапазон от start, включительно, до end, не включительно, этого списка.
final list1 = <int>[1, 2, 3, 4]; final list2 = <int>[5, 6, 7, 8, 9]; // Copies the 4th and 5th items in list2 as the 2nd and 3rd items // of list1. const skipCount = 3; list1.setRange(1, 3, list2, skipCount); print(list1); // [1, 8, 9, 4]
Указанный диапазон, заданный start и end, должен быть допустимым. Диапазон от start до end является допустимым, если 0 ≤ start ≤ end ≤ length. Пустой диапазон (с end == start) является допустимым.
iterable должен содержать достаточно объектов, чтобы заполнить диапазон от start до end после пропуска skipCount объектов.
Если iterable — это этот список, операция правильно копирует элементы, изначально находящиеся в диапазоне от skipCount до skipCount + (end - start) в диапазон start до end, даже если два диапазона перекрываются.
Если iterable зависит от этого списка каким-либо другим образом, никаких гарантий не предоставляется.
Реализация
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
RangeError.checkValidRange(start, end, this.length);
int length = end - start;
if (length == 0) return;
RangeError.checkNotNegative(skipCount, "skipCount");
List<E> otherList;
int otherStart;
// TODO(floitsch): Make this accept more.
if (iterable is List<E>) {
otherList = iterable;
otherStart = skipCount;
} else {
otherList = iterable.skip(skipCount).toList(growable: false);
otherStart = 0;
}
if (otherStart + length > otherList.length) {
throw IterableElementError.tooFew();
}
if (otherStart < start) {
// Copy backwards to ensure correct copy if [from] is this.
for (int i = length - 1; i >= 0; i--) {
this[start + i] = otherList[otherStart + i];
}
} else {
for (int i = 0; i < length; i++) {
this[start + i] = otherList[otherStart + i];
}
}
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-collection/ListMixin/setRange.html