swap method

List<T> swap(
  1. int i,
  2. int j
)

Creates a new List with the elements at positions i and j swapped.

The function creates a new List by converting the current Iterable to a List. It then swaps the elements at the specified positions i and j by using a temporary variable. The function returns the new List with the swapped elements.

Returns a new List with the elements at positions i and j swapped.

Example:

List<int> numbers = [1, 2, 3, 4, 5];
List<int> swappedNumbers = numbers.swap(1, 3);
print(swappedNumbers); // Output: [1, 4, 3, 2, 5]

Implementation

List<T> swap(int i, int j) {
  final list = this.toList();
  final aux = list[i];
  list[i] = list[j];
  list[j] = aux;
  return list;
}