Spec-Zone.ru › Dart 2

dart:collection

Метод lastIndexOf

int lastIndexOf(
  1. Object? element,
  2. [int? start]
)
override

Индекс последнего вхождения element в этом списке.

Ищет в списке в обратном порядке от индекса start до 0.

В первый раз, когда встречается объект o, так что o == element, возвращается индекс o.

final notes = <String>['do', 're', 'mi', 're'];
const startIndex = 2;
final index = notes.lastIndexOf('re', startIndex); // 1

Если start не указано, этот метод ищет с конца списка.

final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexOf('re'); // 3

Возвращает -1, если element не найден.

final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexOf('fa'); // -1

Реализация

int lastIndexOf(Object? element, [int? start]) {
  if (start == null || start >= this.length) start = this.length - 1;

  // TODO(38493): The previous line should promote.
  if (start == null) throw "!";

  for (int i = start; i >= 0; i--) {
    if (this[i] == element) return i;
  }
  return -1;
}

© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-collection/ListMixin/lastIndexOf.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API