List<T>.FindLastIndex 方法

定義

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中或其中一部分中最後一次出現之以零為起始的索引。

多載

FindLastIndex(Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回整個 List<T> 內最後一次出現之以為零起始的索引。

FindLastIndex(Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中從第一個項目延伸到指定之索引的項目範圍內,最後一個符合項目之以零為起始的索引。

FindLastIndex(Int32, Int32, Predicate<T>)

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中包含指定之項目數目,且結束於指定之索引的項目範圍內最後一個符合項目之以零為起始的索引。

FindLastIndex(Predicate<T>)

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

搜尋符合指定之述詞所定義的條件之項目,並傳回整個 List<T> 內最後一次出現之以為零起始的索引。

C#
public int FindLastIndex (Predicate<T> match);

參數

match
Predicate<T>

定義要搜尋項目之條件的 Predicate<T> 委派。

傳回

符合 match 所定義條件的元素,最後一次出現項目之以零為起始的索引 (若有找到),否則為 -1。

例外狀況

matchnull

範例

下列範例示範 類別的 List<T> find 方法。 類別的List<T>範例包含 book 類別的物件Book,使用範例 XML 檔案中的數據:書籍 (LINQ to XML) 。 範例FillList中的 方法會使用 LINQ to XML,將 XML 中的值剖析為 對象的屬性值book

下表描述為尋找方法提供的範例。

方法 範例
Find(Predicate<T>) 使用 IDToFind 述詞委派依標識符尋找書籍。

C# 範例使用匿名委派。
FindAll(Predicate<T>) 使用述詞委派尋找其 Genre 屬性為 「Computer」 FindComputer 的所有書籍。
FindLast(Predicate<T>) 使用 PubBefore2001 述詞委派,尋找在 2001 年之前具有發行日期之集合中的最後一本書。

C# 範例使用匿名委派。
FindIndex(Predicate<T>) 使用述詞委派尋找第一部計算機書籍的 FindComputer 索引。
FindLastIndex(Predicate<T>) 使用述詞委派尋找最後一部計算機書籍的 FindComputer 索引。
FindIndex(Int32, Int32, Predicate<T>) 使用 FindComputer 述詞委派,在集合的第二半尋找第一部計算機書籍的索引。
FindLastIndex(Int32, Int32, Predicate<T>) 使用 FindComputer 述詞委派,在集合的第二半尋找最後一部計算機書籍的索引。
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Find
{
    class Program
    {
        private static string IDtoFind = "bk109";

        private static List<Book> Books = new List<Book>();
        public static void Main(string[] args)
        {
            FillList();

            // Find a book by its ID.
            Book result = Books.Find(
            delegate(Book bk)
            {
                return bk.ID == IDtoFind;
            }
            );
            if (result != null)
            {
                DisplayResult(result, "Find by ID: " + IDtoFind);
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find last book in collection published before 2001.
            result = Books.FindLast(
            delegate(Book bk)
            {
                DateTime year2001 = new DateTime(2001,01,01);
                return bk.Publish_date < year2001;
            });
            if (result != null)
            {
                DisplayResult(result, "Last book in collection published before 2001:");
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find all computer books.
            List<Book> results = Books.FindAll(FindComputer);
            if (results.Count != 0)
            {
                DisplayResults(results, "All computer:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find all books under $10.00.
            results = Books.FindAll(
            delegate(Book bk)
            {
                return bk.Price < 10.00;
            }
            );
            if (results.Count != 0)
            {
                DisplayResults(results, "Books under $10:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find index values.
            Console.WriteLine();
            int ndx = Books.FindIndex(FindComputer);
            Console.WriteLine("Index of first computer book: {0}", ndx);
            ndx = Books.FindLastIndex(FindComputer);
            Console.WriteLine("Index of last computer book: {0}", ndx);

            int mid = Books.Count / 2;
            ndx = Books.FindIndex(mid, mid, FindComputer);
            Console.WriteLine("Index of first computer book in the second half of the collection: {0}", ndx);

            ndx = Books.FindLastIndex(Books.Count - 1, mid, FindComputer);
            Console.WriteLine("Index of last computer book in the second half of the collection: {0}", ndx);
        }

        // Populates the list with sample data.
        private static void FillList()
        {

            // Create XML elements from a source file.
            XElement xTree = XElement.Load(@"c:\temp\books.xml");

            // Create an enumerable collection of the elements.
            IEnumerable<XElement> elements = xTree.Elements();

            // Evaluate each element and set set values in the book object.
            foreach (XElement el in elements)
            {
                Book book = new Book();
                book.ID = el.Attribute("id").Value;
                IEnumerable<XElement> props = el.Elements();
                foreach (XElement p in props)
                {

                    if (p.Name.ToString().ToLower() == "author")
                    {
                        book.Author = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "title")
                    {
                        book.Title = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "genre")
                    {
                        book.Genre = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "price")
                    {
                        book.Price = Convert.ToDouble(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "publish_date")
                    {
                        book.Publish_date = Convert.ToDateTime(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "description")
                    {
                        book.Description = p.Value;
                    }
                }

                Books.Add(book);
            }

            DisplayResults(Books, "All books:");
        }

        // Explicit predicate delegate.
        private static bool FindComputer(Book bk)
        {

            if (bk.Genre == "Computer")
            {
                return true;
            }
        else
            {
                return false;
            }
        }

        private static void DisplayResult(Book result, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", result.ID,
                result.Author, result.Title, result.Genre, result.Price,
                result.Publish_date.ToShortDateString());
            Console.WriteLine();
        }

        private static void DisplayResults(List<Book> results, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            foreach (Book b in results)
            {

                Console.Write("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", b.ID,
                    b.Author, b.Title, b.Genre, b.Price,
                    b.Publish_date.ToShortDateString());
            }
            Console.WriteLine();
        }
    }

    public class Book
    {
        public string ID { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
        public double Price { get; set; }
        public DateTime Publish_date { get; set; }
        public string Description { get; set; }
    }
}

備註

List<T> 從最後一個項目開始向後搜尋,並在第一個元素結束。

Predicate<T>是方法的委派,如果傳遞給它的物件符合委派中定義的條件,則會傳回 true 。 目前 List<T> 的專案會個別傳遞至 Predicate<T> 委派。

這個方法會執行線性搜尋;因此,這個方法是 o (n) 運算,其中 nCount

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

FindLastIndex(Int32, Predicate<T>)

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

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中從第一個項目延伸到指定之索引的項目範圍內,最後一個符合項目之以零為起始的索引。

C#
public int FindLastIndex (int startIndex, Predicate<T> match);

參數

startIndex
Int32

向後搜尋之以零為起始的起始索引。

match
Predicate<T>

定義要搜尋項目之條件的 Predicate<T> 委派。

傳回

符合 match 所定義條件的元素,最後一次出現項目之以零為起始的索引 (若有找到),否則為 -1。

例外狀況

matchnull

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

備註

List<T>startIndex 第一個元素開始向後搜尋 , 並結束。

Predicate<T>是方法的委派,如果傳遞給它的物件符合委派中定義的條件,則會傳回 true 。 目前 List<T> 的專案會個別傳遞至 Predicate<T> 委派。

這個方法會執行線性搜尋;因此,這個方法是 O (n) 運算,其中 n 是 從 開頭 List<T>startIndex的項目數目。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

FindLastIndex(Int32, Int32, Predicate<T>)

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

搜尋符合指定之述詞所定義的條件之項目,並傳回 List<T> 中包含指定之項目數目,且結束於指定之索引的項目範圍內最後一個符合項目之以零為起始的索引。

C#
public int FindLastIndex (int startIndex, int count, Predicate<T> match);

參數

startIndex
Int32

向後搜尋之以零為起始的起始索引。

count
Int32

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

match
Predicate<T>

定義要搜尋項目之條件的 Predicate<T> 委派。

傳回

符合 match 所定義條件的元素,最後一次出現項目之以零為起始的索引 (若有找到),否則為 -1。

例外狀況

matchnull

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

-或-

count 小於 0。

-或-

startIndexcount 不指定 List<T> 的有效區段。

範例

下列範例示範 類別的 List<T> find 方法。 類別的List<T>範例包含 book 類別的物件Book,使用範例 XML 檔案中的數據:書籍 (LINQ to XML) 。 範例FillList中的 方法會使用 LINQ to XML,將 XML 中的值剖析為 對象的屬性值book

下表描述為尋找方法提供的範例。

方法 範例
Find(Predicate<T>) 使用 IDToFind 述詞委派依標識符尋找書籍。

C# 範例使用匿名委派。
FindAll(Predicate<T>) 使用述詞委派尋找其 Genre 屬性為 「Computer」 FindComputer 的所有書籍。
FindLast(Predicate<T>) 使用 PubBefore2001 述詞委派,尋找在 2001 年之前具有發行日期之集合中的最後一本書。

C# 範例使用匿名委派。
FindIndex(Predicate<T>) 使用述詞委派尋找第一部計算機書籍的 FindComputer 索引。
FindLastIndex(Predicate<T>) 使用述詞委派尋找最後一部計算機書籍的 FindComputer 索引。
FindIndex(Int32, Int32, Predicate<T>) 使用 FindComputer 述詞委派,在集合的第二半尋找第一部計算機書籍的索引。
FindLastIndex(Int32, Int32, Predicate<T>) 使用 FindComputer 述詞委派,在集合的第二半尋找最後一部計算機書籍的索引。
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Find
{
    class Program
    {
        private static string IDtoFind = "bk109";

        private static List<Book> Books = new List<Book>();
        public static void Main(string[] args)
        {
            FillList();

            // Find a book by its ID.
            Book result = Books.Find(
            delegate(Book bk)
            {
                return bk.ID == IDtoFind;
            }
            );
            if (result != null)
            {
                DisplayResult(result, "Find by ID: " + IDtoFind);
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find last book in collection published before 2001.
            result = Books.FindLast(
            delegate(Book bk)
            {
                DateTime year2001 = new DateTime(2001,01,01);
                return bk.Publish_date < year2001;
            });
            if (result != null)
            {
                DisplayResult(result, "Last book in collection published before 2001:");
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find all computer books.
            List<Book> results = Books.FindAll(FindComputer);
            if (results.Count != 0)
            {
                DisplayResults(results, "All computer:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find all books under $10.00.
            results = Books.FindAll(
            delegate(Book bk)
            {
                return bk.Price < 10.00;
            }
            );
            if (results.Count != 0)
            {
                DisplayResults(results, "Books under $10:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find index values.
            Console.WriteLine();
            int ndx = Books.FindIndex(FindComputer);
            Console.WriteLine("Index of first computer book: {0}", ndx);
            ndx = Books.FindLastIndex(FindComputer);
            Console.WriteLine("Index of last computer book: {0}", ndx);

            int mid = Books.Count / 2;
            ndx = Books.FindIndex(mid, mid, FindComputer);
            Console.WriteLine("Index of first computer book in the second half of the collection: {0}", ndx);

            ndx = Books.FindLastIndex(Books.Count - 1, mid, FindComputer);
            Console.WriteLine("Index of last computer book in the second half of the collection: {0}", ndx);
        }

        // Populates the list with sample data.
        private static void FillList()
        {

            // Create XML elements from a source file.
            XElement xTree = XElement.Load(@"c:\temp\books.xml");

            // Create an enumerable collection of the elements.
            IEnumerable<XElement> elements = xTree.Elements();

            // Evaluate each element and set set values in the book object.
            foreach (XElement el in elements)
            {
                Book book = new Book();
                book.ID = el.Attribute("id").Value;
                IEnumerable<XElement> props = el.Elements();
                foreach (XElement p in props)
                {

                    if (p.Name.ToString().ToLower() == "author")
                    {
                        book.Author = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "title")
                    {
                        book.Title = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "genre")
                    {
                        book.Genre = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "price")
                    {
                        book.Price = Convert.ToDouble(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "publish_date")
                    {
                        book.Publish_date = Convert.ToDateTime(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "description")
                    {
                        book.Description = p.Value;
                    }
                }

                Books.Add(book);
            }

            DisplayResults(Books, "All books:");
        }

        // Explicit predicate delegate.
        private static bool FindComputer(Book bk)
        {

            if (bk.Genre == "Computer")
            {
                return true;
            }
        else
            {
                return false;
            }
        }

        private static void DisplayResult(Book result, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", result.ID,
                result.Author, result.Title, result.Genre, result.Price,
                result.Publish_date.ToShortDateString());
            Console.WriteLine();
        }

        private static void DisplayResults(List<Book> results, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            foreach (Book b in results)
            {

                Console.Write("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", b.ID,
                    b.Author, b.Title, b.Genre, b.Price,
                    b.Publish_date.ToShortDateString());
            }
            Console.WriteLine();
        }
    }

    public class Book
    {
        public string ID { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
        public double Price { get; set; }
        public DateTime Publish_date { get; set; }
        public string Description { get; set; }
    }
}

備註

如果 大於 0,則會List<T>從 開始向後startIndex搜尋 ,count且結尾為startIndex減號count加上 1。

Predicate<T>是方法的委派,如果傳遞給它的物件符合委派中定義的條件,則會傳回 true 。 目前 List<T> 的專案會個別傳遞至 Predicate<T> 委派。

這個方法會執行線性搜尋;因此,這個方法是 o (n) 運算,其中 ncount

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0