swap method
Swaps the elements at index1 and index2.
Throws a RangeError if indices are out of bounds.
Implementation
void swap(int index1, int index2) {
if (index1 < 0 || index1 >= length || index2 < 0 || index2 >= length) {
throw RangeError('Indices are out of bounds');
}
final temp = this[index1];
this[index1] = this[index2];
this[index2] = temp;
}