語言

List<T>.LastIndexOf 方法

定義

回傳該值最後一次出現於 List<T> 或部分的零基索引。

多載

名稱 Description
LastIndexOf(T)

搜尋指定的物件,並回傳整個 List<T>中最後一次出現的零基索引。

LastIndexOf(T, Int32)

搜尋指定的物件,並回傳從第一個元素延伸到指定索引範圍內最後一次出現 List<T> 的零為基礎的索引。

LastIndexOf(T, Int32, Int32)

搜尋指定的物件,並回 List<T> 傳包含指定元素數量且結束於指定索引範圍內最後一次出現的零為基礎的索引。

範例

以下範例展示了該 LastIndexOf 方法的三種超載方式。 List<T>建立一個字串,其中一個項目出現兩次,分別位於索引位置 0 和索引位置 5。 LastIndexOf(T)方法過載會從最後搜尋整個清單,並找到該字串的第二個出現點。 LastIndexOf(T, Int32)方法過載用於從索引位置 3 開始,向後搜尋清單,直到列表開頭,以找到該字串在列表中的首次出現。 最後, LastIndexOf(T, Int32, Int32) 方法過載用於搜尋四個項目的範圍,從索引位置 4 開始往後延伸(即搜尋位置 4、3、2 和 1 的項目);此搜尋回傳 -1,因為該範圍內沒有搜尋字串的實例。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Brachiosaurus");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nLastIndexOf(\"Tyrannosaurus\"): {0}",
            dinosaurs.LastIndexOf("Tyrannosaurus"));

        Console.WriteLine("\nLastIndexOf(\"Tyrannosaurus\", 3): {0}",
            dinosaurs.LastIndexOf("Tyrannosaurus", 3));

        Console.WriteLine("\nLastIndexOf(\"Tyrannosaurus\", 4, 4): {0}",
            dinosaurs.LastIndexOf("Tyrannosaurus", 4, 4));
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

LastIndexOf("Tyrannosaurus"): 5

LastIndexOf("Tyrannosaurus", 3): 0

LastIndexOf("Tyrannosaurus", 4, 4): -1
 */
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Brachiosaurus")
        dinosaurs.Add("Deinonychus")
        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Compsognathus")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "LastIndexOf(""Tyrannosaurus""): {0}", _
            dinosaurs.LastIndexOf("Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "LastIndexOf(""Tyrannosaurus"", 3): {0}", _
            dinosaurs.LastIndexOf("Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "LastIndexOf(""Tyrannosaurus"", 4, 4): {0}", _
            dinosaurs.LastIndexOf("Tyrannosaurus", 4, 4))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'LastIndexOf("Tyrannosaurus"): 5
'
'LastIndexOf("Tyrannosaurus", 3): 0
'
'LastIndexOf("Tyrannosaurus", 4, 4): -1

LastIndexOf(T)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

搜尋指定的物件,並回傳整個 List<T>中最後一次出現的零基索引。

public:
 int LastIndexOf(T item);
public int LastIndexOf(T item);
member this.LastIndexOf : 'T -> int
Public Function LastIndexOf (item As T) As Integer

參數

item
T

要定位於 List<T>的物件。 值可以是 null 參考型別。

傳回

在整個 中item最後一次出現的List<T>零基索引(若找到);否則為 -1。

備註

List<T>從最後一個元素開始,從第一個元素開始,向後搜尋。

此方法利用預設的等式比較器來判定相等,該比較器EqualityComparer<T>.DefaultT是列表中的值類型。

此方法執行線性搜尋;因此,此方法是一個 O(n) 運算,其中 n 為 Count。

另請參閱

適用於

LastIndexOf(T, Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

搜尋指定的物件,並回傳從第一個元素延伸到指定索引範圍內最後一次出現 List<T> 的零為基礎的索引。

public:
 int LastIndexOf(T item, int index);
public int LastIndexOf(T item, int index);
member this.LastIndexOf : 'T * int -> int
Public Function LastIndexOf (item As T, index As Integer) As Integer

參數

item
T

要定位於 List<T>的物件。 值可以是 null 參考型別。

index
Int32

回溯搜尋之以零起始的起始索引。

傳回

在元素範圍內最後一次出現的itemList<T>零基索引,若找到,則從第一個元素延伸至index;否則為 -1。

例外狀況

index 超出有效索引 List<T>範圍。

備註

List<T>從第一個元素開始index並從第一個元素開始,向後搜尋。

此方法利用預設的等式比較器來判定相等,該比較器EqualityComparer<T>.DefaultT是列表中的值類型。

此方法執行線性搜尋;因此,此方法是一個 O(n) 運算,其中 n 是從 到List<T>的index元素數。

另請參閱

適用於

LastIndexOf(T, Int32, Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

搜尋指定的物件,並回 List<T> 傳包含指定元素數量且結束於指定索引範圍內最後一次出現的零為基礎的索引。

public:
 int LastIndexOf(T item, int index, int count);
public int LastIndexOf(T item, int index, int count);
member this.LastIndexOf : 'T * int * int -> int
Public Function LastIndexOf (item As T, index As Integer, count As Integer) As Integer

參數

item
T

要定位於 List<T>的物件。 值可以是 null 參考型別。

index
Int32

回溯搜尋之以零起始的起始索引。

count
Int32

要搜尋之區段中的項目數目。

傳回

在元素範圍內itemList<T>最後一次出現的count零基索引,包含元素數,若找到,則在 結束;index否則為 -1。

例外狀況

index 超出有效索引 List<T>範圍。

-或-

count 小於0。

-或-

index且count不指定有效區段。List<T>

備註

List<T>當 index 大於 0 時,則從 開始index至結束count於負count加 1 的逆向搜尋。

此方法利用預設的等式比較器來判定相等,該比較器EqualityComparer<T>.DefaultT是列表中的值類型。

此方法執行線性搜尋;因此,此方法是一個 O(n) 運算,其中 n 為 count。

另請參閱

適用於