Array.Exists<T>(T[], Predicate<T>) メソッド

定義

指定された配列に、指定された述語によって定義された条件と一致する要素が含まれているかどうかを判断します。

public:
generic <typename T>
 static bool Exists(cli::array <T> ^ array, Predicate<T> ^ match);
public static bool Exists<T> (T[] array, Predicate<T> match);
static member Exists : 'T[] * Predicate<'T> -> bool
Public Shared Function Exists(Of T) (array As T(), match As Predicate(Of T)) As Boolean

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の Array。インデックス番号は 0 から始まります。

match
Predicate<T>

検索する要素の条件を定義する Predicate<T>

戻り値

Boolean

指定された述語によって定義された条件と一致する 1 つ以上の要素が array に存在する場合は、true。それ以外の場合は false

例外

arraynullです。

または

matchnullです。

次の例では、ラムダ式を使用して、惑星が指定された文字で始まるかどうか、または指定された配列で惑星が見つかったかどうかを確認するメソッドの一致条件 Exists を指定します。

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] planets = { "Mercury", "Venus",
                "Earth", "Mars", "Jupiter",
                "Saturn", "Uranus", "Neptune" };

            Console.WriteLine("One or more planets begin with 'M': {0}",
                Array.Exists(planets, element => element.StartsWith("M")));

            Console.WriteLine("One or more planets begin with 'T': {0}",
                Array.Exists(planets, element => element.StartsWith("T")));

            Console.WriteLine("Is Pluto one of the planets? {0}",
                Array.Exists(planets, element => element == "Pluto"));
        }
    }
}
// The example displays the following output:
//       One or more planets begin with 'M': True
//       One or more planets begin with 'T': False
//       Is Pluto one of the planets? False
open System

let  planets = 
    [| "Mercury"; "Venus"
       "Earth"; "Mars"; "Jupiter"
       "Saturn"; "Uranus"; "Neptune" |]

Array.Exists(planets, fun element -> element.StartsWith "M")
|> printfn "One or more planets begin with 'M': %O"

Array.Exists(planets, fun element -> element.StartsWith "T")
|> printfn "One or more planets begin with 'T': %O"

Array.Exists(planets, fun element -> element = "Pluto")
|> printfn "Is Pluto one of the planets? %O"

// The example displays the following output:
//       One or more planets begin with 'M': True
//       One or more planets begin with 'T': False
//       Is Pluto one of the planets? False
Module Example
    Public Sub Main()
        Dim planets() As String = {"Mercury", "Venus",
                                    "Earth", "Mars", "Jupiter",
                                    "Saturn", "Uranus", "Neptune"}

        Console.WriteLine("One or more planets begin with 'M': {0}",
            Array.Exists(planets, Function(element)
                                      Return element.StartsWith("M")
                                  End Function))

        Console.WriteLine("One or more planets begin with 'T': {0}",
            Array.Exists(planets, Function(element)
                                      Return element.StartsWith("T")
                                  End Function))

        Console.WriteLine("Is Pluto one of the planets? {0}",
            Array.Exists(planets, Function(element)
                                      Return element.Equals("Pluto")
                                  End Function))

    End Sub
End Module
' The example displays the following output:
'       One or more planets begin with 'M': True
'       One or more planets begin with 'T': False
'       Is Pluto one of the planets? False

次の例では、このメソッドを Exists 使用して、文字列配列内の名前が指定した文字で始まるかどうかを示します。 この例では、 StringSearcher 検索する文字列をクラス コンストラクターに渡すことによってオブジェクトをインスタンス化します。 メソッドのシグネチャは StringSearcher.StartsWith デリゲートと Predicate<T> 同じです。 メソッドが Exists 呼び出されると、配列内のすべての要素が返 true されるか反復処理されるまで、配列の各メンバーがデリゲートに渡されます。

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, (new StringSearcher(charToFind)).StartsWith));
   }
}

public class StringSearcher
{
   char firstChar;

   public StringSearcher(char firstChar)
   {
      this.firstChar = char.ToUpper(firstChar);
   }

   public bool StartsWith(string s)
   {
      if (string.IsNullOrEmpty(s)) return false;

      if(s.Substring(0, 1).ToUpper() == firstChar.ToString())
         return true;
      else
         return false;
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
open System

type StringSearcher(firstChar) =
    member _.StartsWith(s) =
        if String.IsNullOrEmpty s then 
            false 
        else
            s.Substring(0, 1).ToUpper() = string firstChar

let names = 
    [| "Adam"; "Adel"; "Bridgette"; "Carla";
       "Charles"; "Daniel"; "Elaine"; "Frances"
       "George"; "Gillian"; "Henry"; "Irving"
       "James"; "Janae"; "Lawrence"; "Miguel"
       "Nicole"; "Oliver"; "Paula"; "Robert"
       "Stephen"; "Thomas"; "Vanessa"
       "Veronica"; "Wilberforce" |]

let charsToFind = [ 'A'; 'K'; 'W'; 'Z' ]

for char in charsToFind do 
    let exists = Array.Exists(names, fun x -> StringSearcher(char).StartsWith x)
    // let exists = Array.exists (StringSearcher(char).StartsWith) names
    printfn $"One or more names begin with '{char}': {exists}"

// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
Module Example
   Public Sub Main()
      Dim names() As String = { "Adam", "Adel", "Bridgette", "Carla",
                                "Charles", "Daniel", "Elaine", "Frances",
                                "George", "Gillian", "Henry", "Irving",
                                "James", "Janae", "Lawrence", "Miguel",
                                "Nicole", "Oliver", "Paula", "Robert",
                                "Stephen", "Thomas", "Vanessa",
                                "Veronica", "Wilberforce" }
      Dim charsToFind() As Char = { "A"c, "K"c, "W"c, "Z"c }
      
      For Each charToFind In charsToFind
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, AddressOf (New StringSearcher(charToFind)).StartsWith))
      Next
   End Sub
   
End Module

Public Class StringSearcher
   Dim firstChar As Char
   
   Public Sub New(firstChar As Char)
      Me.firstChar = Char.ToUpper(firstChar)
   End Sub
   
   Public Function StartsWith(s As String) As Boolean
      If String.IsNullOrEmpty(s) Then Return False
      
      If s.Substring(0, 1).ToUpper = firstChar Then
         Return True
      Else
         Return False
      End If
   End Function
End Class
' The example displays the following output:
'       One or more names begin with 'A': True
'       One or more names begin with 'K': False
'       One or more names begin with 'W': True
'       One or more names begin with 'Z': False

デリゲートのシグネチャに対応するメソッドを明示的に定義するのではなく、ラムダ式を使用することもできます。 次の例では、クラスとそのStartsWithメソッドをStringSearcherラムダ式に置き換えます。

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names,
                                        s => { if (string.IsNullOrEmpty(s))
                                                  return false;

                                               if (s.Substring(0, 1).ToUpper() == charToFind.ToString())
                                                  return true;
                                               else
                                                  return false;
                                             } ));
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
open System

let names = 
    [| "Adam"; "Adel"; "Bridgette"; "Carla";
       "Charles"; "Daniel"; "Elaine"; "Frances"
       "George"; "Gillian"; "Henry"; "Irving"
       "James"; "Janae"; "Lawrence"; "Miguel"
       "Nicole"; "Oliver"; "Paula"; "Robert"
       "Stephen"; "Thomas"; "Vanessa"
       "Veronica"; "Wilberforce" |]

let charsToFind = [ 'A'; 'K'; 'W'; 'Z' ]

for char in charsToFind do
    let exists = 
        Array.Exists(names, fun s -> 
            if String.IsNullOrEmpty s then false 
            else s.Substring(0, 1).ToUpper() = string char)

    printfn $"One or more names begin with '{char}': {exists}"
                    
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False
Module Example
   Public Sub Main()
      Dim names() As String = { "Adam", "Adel", "Bridgette", "Carla",
                                "Charles", "Daniel", "Elaine", "Frances",
                                "George", "Gillian", "Henry", "Irving",
                                "James", "Janae", "Lawrence", "Miguel",
                                "Nicole", "Oliver", "Paula", "Robert",
                                "Stephen", "Thomas", "Vanessa",
                                "Veronica", "Wilberforce" }
      Dim charsToFind() As Char = { "A"c, "K"c, "W"c, "Z"c }

      For Each charToFind In charsToFind
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, Function(s)
                                                  If String.IsNullOrEmpty(s) Then Return False

                                                  If s.Substring(0, 1).ToUpper = charToFind Then
                                                     Return True
                                                  Else
                                                     Return False
                                                  End If
                                               End Function ))
      Next
   End Sub
End Module
' The example displays the following output:
'       One or more names begin with 'A': True
'       One or more names begin with 'K': False
'       One or more names begin with 'W': True
'       One or more names begin with 'Z': False

注釈

これは Predicate<T> 、渡されたオブジェクトがデリゲートで定義されている条件と一致するかどうかを返す true メソッドへのデリゲートです。 の array 要素は個別にに渡され Predicate<T>、一致が見つかると処理が停止されます。

注意

C# とVisual Basicでは、デリゲートを明示的に作成するPredicate<T>必要はありません。 これらの言語は、コンテキストから正しいデリゲートを推論し、自動的に作成します。 F# では、関数とラムダ式は暗黙的に変換されます。

このメソッドは O(n) 演算で、次narray値を指定しますLength

適用対象

こちらもご覧ください