ArrayList.LastIndexOf Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает отсчитываемый от нуля индекс последнего вхождения значения в ArrayList части или в ее части.
Перегрузки
| Имя | Описание |
|---|---|
| LastIndexOf(Object) |
Выполняет поиск указанного Object и возвращает отсчитываемый от нуля индекс последнего вхождения в течение всего ArrayList. |
| LastIndexOf(Object, Int32) |
Выполняет поиск указанного Object и возвращает отсчитываемый от нуля индекс последнего вхождения в диапазоне элементов, ArrayList который расширяется от первого элемента к указанному индексу. |
| LastIndexOf(Object, Int32, Int32) |
Выполняет поиск указанного Object и возвращает отсчитываемый от нуля индекс последнего вхождения в диапазоне элементов, ArrayList содержащих указанное число элементов и заканчивается на указанном индексе. |
LastIndexOf(Object)
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
public:
virtual int LastIndexOf(System::Object ^ value);
public virtual int LastIndexOf(object value);
public virtual int LastIndexOf(object? value);
abstract member LastIndexOf : obj -> int
override this.LastIndexOf : obj -> int
Public Overridable Function LastIndexOf (value As Object) As Integer
Параметры
Возвращаемое значение
Отсчитываемый от нуля индекс последнего вхождения value в пределах всего ArrayListнайденного; в противном случае — значение -1.
Примеры
В следующем примере кода показано, как определить индекс последнего вхождения указанного элемента.
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList myAL = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
myAL.Add( "in" );
myAL.Add( "the" );
myAL.Add( "barn" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList contains the following values:" );
PrintIndexAndValues( myAL );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = myAL.LastIndexOf( myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf( myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( " [{0}]: {1}", i++, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As New ArrayList()
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
myAL.Add("in")
myAL.Add("the")
myAL.Add("barn")
' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList contains the following values:")
PrintIndexAndValues(myAL)
' Searches for the last occurrence of the duplicated value.
Dim myString As [String] = "the"
Dim myIndex As Integer = myAL.LastIndexOf(myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf(myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf(myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
Dim i as Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The ArrayList contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.
Комментарии
Выполняется ArrayList поиск назад, начиная с последнего элемента и заканчивающийся первым элементом.
Этот метод выполняет линейный поиск; поэтому этот метод является операцией O(n) , где n находится Count.
Этот метод использует объекты Equals коллекции и CompareTo методы item для определения того, существует ли элемент.
См. также раздел
Применяется к
LastIndexOf(Object, Int32)
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
public:
virtual int LastIndexOf(System::Object ^ value, int startIndex);
public virtual int LastIndexOf(object value, int startIndex);
public virtual int LastIndexOf(object? value, int startIndex);
abstract member LastIndexOf : obj * int -> int
override this.LastIndexOf : obj * int -> int
Public Overridable Function LastIndexOf (value As Object, startIndex As Integer) As Integer
Параметры
- startIndex
- Int32
Отсчитываемый от нуля начальный индекс обратного поиска.
Возвращаемое значение
Отсчитываемый от нуля индекс последнего вхождения value в диапазоне элементов, ArrayList который расширяется от первого элемента к startIndex, если он найден; в противном случае — -1.
Исключения
startIndexнаходится за пределами диапазона допустимых индексов для .ArrayList
Примеры
В следующем примере кода показано, как определить индекс последнего вхождения указанного элемента.
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList myAL = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
myAL.Add( "in" );
myAL.Add( "the" );
myAL.Add( "barn" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList contains the following values:" );
PrintIndexAndValues( myAL );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = myAL.LastIndexOf( myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf( myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( " [{0}]: {1}", i++, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As New ArrayList()
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
myAL.Add("in")
myAL.Add("the")
myAL.Add("barn")
' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList contains the following values:")
PrintIndexAndValues(myAL)
' Searches for the last occurrence of the duplicated value.
Dim myString As [String] = "the"
Dim myIndex As Integer = myAL.LastIndexOf(myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf(myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf(myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
Dim i as Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The ArrayList contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.
Комментарии
Выполняется ArrayList поиск назад, начиная startIndex с первого элемента и заканчивающийся.
Этот метод выполняет линейный поиск; Таким образом, этот метод является операцией O(n) , где n число элементов с начала ArrayList до startIndex.
Этот метод определяет равенство путем вызова Object.Equals.
Этот метод использует объекты Equals коллекции и CompareTo методы item для определения того, существует ли элемент.
См. также раздел
Применяется к
LastIndexOf(Object, Int32, Int32)
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
- Исходный код:
- ArrayList.cs
public:
virtual int LastIndexOf(System::Object ^ value, int startIndex, int count);
public virtual int LastIndexOf(object value, int startIndex, int count);
public virtual int LastIndexOf(object? value, int startIndex, int count);
abstract member LastIndexOf : obj * int * int -> int
override this.LastIndexOf : obj * int * int -> int
Public Overridable Function LastIndexOf (value As Object, startIndex As Integer, count As Integer) As Integer
Параметры
- startIndex
- Int32
Отсчитываемый от нуля начальный индекс обратного поиска.
- count
- Int32
Количество элементов в разделе для поиска.
Возвращаемое значение
Отсчитываемый от нуля индекс последнего вхождения value в диапазоне элементов, ArrayList содержащих count число элементов и заканчивается в startIndexслучае обнаружения; в противном случае — значение -1.
Исключения
startIndexнаходится за пределами диапазона допустимых индексов для .ArrayList
–или–
count меньше нуля.
–или–
startIndex и count не указывайте допустимый раздел в элементе ArrayList.
Примеры
В следующем примере кода показано, как определить индекс последнего вхождения указанного элемента. Обратите внимание, что LastIndexOf это обратный поиск, count поэтому должно быть меньше или равно startIndex + 1.
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList myAL = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
myAL.Add( "in" );
myAL.Add( "the" );
myAL.Add( "barn" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList contains the following values:" );
PrintIndexAndValues( myAL );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = myAL.LastIndexOf( myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf( myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 5 is at index {1}.", myString, myIndex );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( " [{0}]: {1}", i++, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: the
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
[9]: in
[10]: the
[11]: barn
The last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 10 and index 5 is at index 10.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList with three elements of the same value.
Dim myAL As New ArrayList()
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
myAL.Add("in")
myAL.Add("the")
myAL.Add("barn")
' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList contains the following values:")
PrintIndexAndValues(myAL)
' Searches for the last occurrence of the duplicated value.
Dim myString As [String] = "the"
Dim myIndex As Integer = myAL.LastIndexOf(myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
myIndex = myAL.LastIndexOf(myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section of the ArrayList. Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf(myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
Dim i as Integer
Dim obj As [Object]
For Each obj In myList
Console.WriteLine(" [{0}]: {1}", i, obj)
i = i + 1
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The ArrayList contains the following values:
' [0]: the
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
' [9]: in
' [10]: the
' [11]: barn
'
' The last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 10 and index 5 is at index 10.
Комментарии
Выполняется ArrayList поиск назад, начиная startIndex с startIndex минус count плюс 1, если count больше 0.
Этот метод выполняет линейный поиск; поэтому этот метод является операцией O(n) , где n находится count.
Этот метод определяет равенство путем вызова Object.Equals.
Этот метод использует объекты Equals коллекции и CompareTo методы item для определения того, существует ли элемент.