The syntax of List.setRange() method is:
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0])
This setRange() method of List writes some elements of iterable
into a range of this list.
Parameter | Optional/Required | Description |
---|---|---|
start | required | the index in this list at which to start setting elements |
end | required | the index in this list before which to stop setting elements |
iterable | required | the iterable object containing elements to be set in this list |
skipCount | optional [default value is 0] | the number of elements to skip in the iterable before setting elements in this list |
List.setRange() returns value of type void
.
In this example,
numbers
containing integers.replacement
containing integers to replace elements in numbers
.setRange
to replace elements in the range [1, 3)
of numbers
with elements from replacement
.numbers
list.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<int> replacement = [10, 20];
numbers.setRange(1, 3, replacement);
print(numbers);
}
[1, 10, 20, 4, 5]
In this example,
characters
containing characters.replacement
containing characters to replace elements in characters
.setRange
to replace elements in the range [2, 4)
of characters
with elements from replacement
.characters
list.void main() {
List<String> characters = ['a', 'b', 'c', 'd', 'e'];
List<String> replacement = ['x', 'y', 'z'];
characters.setRange(2, 4, replacement);
print(characters);
}
[a, b, x, y, e]
In this example,
words
containing strings.replacement
containing strings to replace elements in words
.setRange
to replace elements in the range [1, 3)
of words
with elements from replacement
.words
list.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date'];
List<String> replacement = ['grape', 'kiwi'];
words.setRange(1, 3, replacement);
print(words);
}
[apple, grape, kiwi, date]
In this Dart tutorial, we learned about setRange() method of List: the syntax and few working examples with output and detailed explanation for each example.