ArrayList.LastIndexOf Methode
Definition
Überlädt
LastIndexOf(Object) |
Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb der gesamten ArrayList zurück.Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList. |
LastIndexOf(Object, Int32) |
Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index. |
LastIndexOf(Object, Int32, Int32) |
Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that contains the specified number of elements and ends at the specified index. |
LastIndexOf(Object)
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
Parameter
- value
- Object
Das Object, das in der ArrayList gesucht werden soll.The Object to locate in the ArrayList. Der Wert kann null
sein.The value can be null
.
Gibt zurück
Der nullbasierte Index des letzten Vorkommens von value
in der gesamten ArrayList; andernfalls –1.The zero-based index of the last occurrence of value
within the entire the ArrayList, if found; otherwise, -1.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird.The following code example shows how to determine the index of the last occurrence of a specified element.
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
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.
*/
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.
Hinweise
Der ArrayList wird rückwärts nach dem letzten Element und mit dem ersten Element durchsucht.The ArrayList is searched backward starting at the last element and ending at the first element.
Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n)
Vorgang, bei dem gleich n
ist Count .This method performs a linear search; therefore, this method is an O(n)
operation, where n
is Count.
Beginnend mit dem .NET Framework 2,0 verwendet diese Methode die-und-Methoden der-Auflistung, Equals CompareTo item
um zu bestimmen, ob ein Element vorhanden ist.Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item
to determine whether item exists. In früheren Versionen der .NET Framework wurde diese Bestimmung durch die Verwendung der Equals -Methode und der- CompareTo Methode des- item
Parameters für die-Objekte in der-Auflistung vorgenommen.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item
parameter on the objects in the collection.
Siehe auch
- IndexOf(Object)
- Contains(Object)
- Durchführen kulturunabhängiger Zeichenfolgenoperationen in SammlungenPerforming Culture-Insensitive String Operations in Collections
Gilt für:
LastIndexOf(Object, Int32)
Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der sich vom ersten Element bis zum angegebenen Index erstreckt.Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index.
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
Parameter
- value
- Object
Das Object, das in der ArrayList gesucht werden soll.The Object to locate in the ArrayList. Der Wert kann null
sein.The value can be null
.
- startIndex
- Int32
Der nullbasierte Startindex für die Rückwärtssuche.The zero-based starting index of the backward search.
Gibt zurück
Der nullbasierte Index des letzten Vorkommens von value
innerhalb des Bereichs von Elementen in der ArrayList, der sich vom ersten Element bis startIndex
erstreckt, sofern gefunden; andernfalls –1.The zero-based index of the last occurrence of value
within the range of elements in the ArrayList that extends from the first element to startIndex
, if found; otherwise, -1.
Ausnahmen
startIndex
liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.startIndex
is outside the range of valid indexes for the ArrayList.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird.The following code example shows how to determine the index of the last occurrence of a specified element.
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
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.
*/
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.
Hinweise
Das ArrayList -Element wird nach hinten startIndex
und am ersten Element durchsucht.The ArrayList is searched backward starting at startIndex
and ending at the first element.
Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n)
Vorgang, wobei n
die Anzahl der Elemente vom Anfang der ArrayList bis zur ist startIndex
.This method performs a linear search; therefore, this method is an O(n)
operation, where n
is the number of elements from the beginning of the ArrayList to startIndex
.
Diese Methode bestimmt Gleichheit durch Aufrufen von Object.Equals .This method determines equality by calling Object.Equals.
Beginnend mit dem .NET Framework 2,0 verwendet diese Methode die-und-Methoden der-Auflistung, Equals CompareTo item
um zu bestimmen, ob ein Element vorhanden ist.Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item
to determine whether item exists. In früheren Versionen der .NET Framework wurde diese Bestimmung durch die Verwendung der Equals -Methode und der- CompareTo Methode des- item
Parameters für die-Objekte in der-Auflistung vorgenommen.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item
parameter on the objects in the collection.
Siehe auch
- IndexOf(Object)
- Contains(Object)
- Durchführen kulturunabhängiger Zeichenfolgenoperationen in SammlungenPerforming Culture-Insensitive String Operations in Collections
Gilt für:
LastIndexOf(Object, Int32, Int32)
Sucht nach dem angegebenen Object und gibt den nullbasierten Index des letzten Vorkommens innerhalb des Bereichs von Elementen in der ArrayList zurück, der die angegebene Anzahl von Elementen enthält und am angegebenen Index endet.Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that contains the specified number of elements and ends at the specified index.
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
Parameter
- value
- Object
Das Object, das in der ArrayList gesucht werden soll.The Object to locate in the ArrayList. Der Wert kann null
sein.The value can be null
.
- startIndex
- Int32
Der nullbasierte Startindex für die Rückwärtssuche.The zero-based starting index of the backward search.
- count
- Int32
Die Anzahl der Elemente im zu durchsuchenden Abschnitt.The number of elements in the section to search.
Gibt zurück
Der nullbasierte Index des letzten Vorkommens von value
innerhalb des Bereichs von Elementen in der ArrayList, der count
Anzahl von Elementen enthält und am startIndex
endet, sofern gefunden; andernfalls –1.The zero-based index of the last occurrence of value
within the range of elements in the ArrayList that contains count
number of elements and ends at startIndex
, if found; otherwise, -1.
Ausnahmen
startIndex
liegt außerhalb des Bereichs der gültigen Indizes für das ArrayList.startIndex
is outside the range of valid indexes for the ArrayList.
- oder --or-
count
ist kleiner als Null.count
is less than zero.
- oder --or-
startIndex
und count
geben keinen gültigen Abschnitt in der ArrayList an.startIndex
and count
do not specify a valid section in the ArrayList.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie der Index des letzten Vorkommens eines angegebenen Elements bestimmt wird.The following code example shows how to determine the index of the last occurrence of a specified element. Beachten Sie, dass LastIndexOf
eine Rückwärtssuche ist, daher count
muss kleiner oder gleich startIndex
+ 1 sein.Note that LastIndexOf
is a backward search; therefore, count
must be less than or equal to startIndex
+ 1.
using namespace System;
using namespace System::Collections;
void PrintIndexAndValues( IEnumerable^ myList );
int main()
{
// Creates and initializes a new ArrayList with three elements of the same value.
ArrayList^ myAL = gcnew 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 );
}
void PrintIndexAndValues( IEnumerable^ myList )
{
int i = 0;
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
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.
*/
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.
Hinweise
Der ArrayList wird nach hinten durchsucht startIndex
, beginnend bei und endet bei startIndex
minus count
plus 1, wenn count
größer als 0 ist.The ArrayList is searched backward starting at startIndex
and ending at startIndex
minus count
plus 1, if count
is greater than 0.
Diese Methode führt eine lineare Suche aus. Daher ist diese Methode ein O(n)
Vorgang, bei dem gleich n
ist count
.This method performs a linear search; therefore, this method is an O(n)
operation, where n
is count
.
Diese Methode bestimmt Gleichheit durch Aufrufen von Object.Equals .This method determines equality by calling Object.Equals.
Beginnend mit dem .NET Framework 2,0 verwendet diese Methode die-und-Methoden der-Auflistung, Equals CompareTo item
um zu bestimmen, ob ein Element vorhanden ist.Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item
to determine whether item exists. In früheren Versionen der .NET Framework wurde diese Bestimmung durch die Verwendung der Equals -Methode und der- CompareTo Methode des- item
Parameters für die-Objekte in der-Auflistung vorgenommen.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item
parameter on the objects in the collection.
Siehe auch
- IndexOf(Object)
- Contains(Object)
- Durchführen kulturunabhängiger Zeichenfolgenoperationen in SammlungenPerforming Culture-Insensitive String Operations in Collections