takeLast method

KtList<T> takeLast(
  1. int n
)

Returns a list containing last n elements.

Implementation

KtList<T> takeLast(int n) {
  if (n < 0) {
    throw ArgumentError("Requested element count $n is less than zero.");
  }
  if (n == 0) return emptyList();
  if (n >= size) return toList();
  if (n == 1) return listFrom([last()]);
  final list = mutableListOf<T>();
  for (var i = size - n; i < size; i++) {
    list.add(get(i));
  }
  return list;
}