sublist method
Returns a new list containing the objects from start
inclusive to end
exclusive.
List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
colors.sublist(1, 3); // ['green', 'blue']
If end
is omitted, the length of this
is used.
colors.sublist(1); // ['green', 'blue', 'orange', 'pink']
An error occurs if start
is outside the range 0
.. length
or if
end
is outside the range start
.. length
.
Implementation
List<E> sublist(int start, [int end]) {
int listLength = this.length;
if (end == null) end = listLength;
RangeError.checkValidRange(start, end, listLength);
int length = end - start;
List<E> result = <E>[]..length = length;
for (int i = 0; i < length; i++) {
result[i] = this[start + i];
}
return result;
}