insertBetween method

List<T> insertBetween(
  1. T element
)

Inserts the same element of type T between each element in a List<T>.

This function takes a list and an element as input. It modifies the original list and adds the given element after each element, except after the last element of the list. Finally, it returns the modified list.

Usage:

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  int element = 0;
  numbers.insertBetween(element);
  print(numbers);  // prints: [1, 0, 2, 0, 3, 0, 4, 0, 5]
}

@param list The original list of elements of type T. @param element The element of type T to be inserted between each element in the list. @return The modified list with the element inserted between each element.

Implementation

List<T> insertBetween(T element) {
  for (int i = length - 1; i > 0; i--) {
    insert(i, element);
  }
  return this;
}