Dart List removeRange()
Syntax & Examples
Syntax of List.removeRange()
The syntax of List.removeRange() method is:
void removeRange(int start, int end)
This removeRange() method of List removes a range of elements from the list.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | the starting index of the range to be removed from the list |
end | required | the ending index of the range to be removed from the list (exclusive) |
Return Type
List.removeRange() returns value of type void
.
✐ Examples
1 Remove elements from index 1 to 2 from the list of numbers
In this example,
- We create a list named
numbers
containing the integers[1, 2, 3, 4, 5]
. - We then use the
removeRange()
method to remove elements from index 1 (value 2) to index 3 (value 3) from the list. - As a result, the list becomes
[1, 4, 5]
. - We print the modified list to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.removeRange(1, 3); // Output: [1, 4, 5]
print(numbers);
}
Output
[1, 4, 5]
2 Remove elements from index 0 to 1 from the list of characters
In this example,
- We create a list named
characters
containing the characters['a', 'b', 'c', 'd']
. - We then use the
removeRange()
method to remove elements from index 0 (value 'a') to index 2 (value 'c') from the list. - As a result, the list becomes
['c', 'd']
. - We print the modified list to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
characters.removeRange(0, 2); // Output: ['c', 'd']
print(characters);
}
Output
[c, d]
3 Remove elements from index 1 to 3 from the list of words
In this example,
- We create a list named
words
containing the strings['apple', 'banana', 'cherry', 'date']
. - We then use the
removeRange()
method to remove elements from index 1 (value 'banana') to index 4 (value 'date') from the list. - As a result, the list becomes
['apple']
. - We print the modified list to standard output.
Dart Program
void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date'];
words.removeRange(1, 4); // Output: ['apple']
print(words);
}
Output
[apple]
Summary
In this Dart tutorial, we learned about removeRange() method of List: the syntax and few working examples with output and detailed explanation for each example.