List<T>.FindLastIndex メソッド

定義

List<T> またはその一部分から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

オーバーロード

FindLastIndex(Predicate<T>)

List<T> 全体から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

FindLastIndex(Int32, Predicate<T>)

List<T> のうち、先頭の要素から指定したインデックスまでの範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

FindLastIndex(Int32, Int32, Predicate<T>)

List<T> のうち、指定したインデックスで終わり、指定した要素数が含まれる範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

FindLastIndex(Predicate<T>)

ソース:
List.cs
ソース:
List.cs
ソース:
List.cs

List<T> 全体から、指定した述語によって定義される条件に一致する要素を検索し、最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

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

パラメーター

match
Predicate<T>

検索する要素の条件を定義する Predicate<T> デリゲート。

戻り値

match で定義された条件と一致する要素が存在する場合、最もインデックス番号の大きい要素の 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

次の例では、 クラスの find メソッドを List<T> 示します。 クラスのList<T>例には、Sample XML File: Books (LINQ to XML) のデータを使用する クラス Bookの オブジェクトが含まれていますbook。 この例の メソッドはFillList、LINQ to XMLを使用して、XML の値をオブジェクトのプロパティ値にbook解析します。

次の表では、find メソッドの例について説明します。

Method
Find(Predicate<T>) 述語デリゲートを使用して、ID で書籍を IDToFind 検索します。

C# の例では、匿名デリゲートを使用します。
FindAll(Predicate<T>) 述語デリゲートを使用して、プロパティが Genre "Computer" であるすべての書籍を FindComputer 検索します。
FindLast(Predicate<T>) 述語デリゲートを使用して、2001 年より前の発行日を持つコレクション内の最後の書籍を PubBefore2001 検索します。

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> のうち、先頭の要素から指定したインデックスまでの範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

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

パラメーター

startIndex
Int32

後方検索の開始位置を示す 0 から始まるインデックス。

match
Predicate<T>

検索する要素の条件を定義する Predicate<T> デリゲート。

戻り値

match で定義された条件と一致する要素が存在する場合、最もインデックス番号の大きい要素の 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

startIndexList<T>の有効なインデックスの範囲外です。

注釈

List<T> 、最初の要素から startIndex 始まり、最初の要素で終わる後方に検索されます。

Predicate<T>は、渡されたオブジェクトがデリゲートで定義されている条件と一致する場合に を返す true メソッドへのデリゲートです。 現在 List<T> の の要素は、デリゲートに個別に Predicate<T> 渡されます。

このメソッドは線形検索を実行します。したがって、このメソッドは O(n) 操作です。ここで、n は の先頭から までのstartIndex要素のList<T>数です。

こちらもご覧ください

適用対象

.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> のうち、指定したインデックスで終わり、指定した要素数が含まれる範囲の中で、指定した述語によって定義される条件に一致する要素を検索し、そのうち最もインデックス番号の大きい要素の 0 から始まるインデックスを返します。

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

パラメーター

startIndex
Int32

後方検索の開始位置を示す 0 から始まるインデックス。

count
Int32

検索対象の範囲内にある要素の数。

match
Predicate<T>

検索する要素の条件を定義する Predicate<T> デリゲート。

戻り値

match で定義された条件と一致する要素が存在する場合、最もインデックス番号の大きい要素の 0 から始まるインデックス。それ以外の場合は -1。

例外

matchnullです。

startIndexList<T>の有効なインデックスの範囲外です。

- または -

count が 0 未満です。

- または -

startIndex および countList<T> 内の正しいセクションを指定していません。

次の例では、 クラスの find メソッドを List<T> 示します。 クラスのList<T>例には、Sample XML File: Books (LINQ to XML) のデータを使用する クラス Bookの オブジェクトが含まれていますbook。 この例の メソッドはFillList、LINQ to XMLを使用して、XML の値をオブジェクトのプロパティ値にbook解析します。

次の表では、find メソッドの例について説明します。

Method
Find(Predicate<T>) 述語デリゲートを使用して、ID で書籍を IDToFind 検索します。

C# の例では、匿名デリゲートを使用します。
FindAll(Predicate<T>) 述語デリゲートを使用して、プロパティが Genre "Computer" であるすべての書籍を FindComputer 検索します。
FindLast(Predicate<T>) 述語デリゲートを使用して、2001 年より前の発行日を持つコレクション内の最後の書籍を PubBefore2001 検索します。

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>が 0 より大きい場合countは、 からstartIndex始まり、マイナス count 1 でstartIndex終わる後方に検索されます。

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