本文整理汇总了C#中System.Array.FindAll<T>方法的典型用法代码示例。如果您正苦于以下问题:C# Array.FindAll<T>方法的具体用法?C# Array.FindAll<T>怎么用?C# Array.FindAll<T>使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Array
的用法示例。
在下文中一共展示了Array.FindAll<T>方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Get an array of n random integers.
int[] values = GetArray(50, 0, 1000);
int lBound = 300;
int uBound = 600;
int[] matchedItems = Array.FindAll(values, x =>
x >= lBound && x <= uBound);
for (int ctr = 0; ctr < matchedItems.Length; ctr++) {
Console.Write("{0} ", matchedItems[ctr]);
if ((ctr + 1) % 12 == 0)
Console.WriteLine();
}
}
private static int[] GetArray(int n, int lower, int upper)
{
Random rnd = new Random();
List<int> list = new List<int>();
for (int ctr = 1; ctr <= n; ctr++)
list.Add(rnd.Next(lower, upper + 1));
return list.ToArray();
}
}
输出:
542 398 356 351 348 301 562 599 575 400 569 306 535 416 393 385
示例2: Main
//引入命名空间
using System;
public class DinoDiscoverySet
{
public static void Main()
{
string[] dinosaurs =
{
"Compsognathus", "Amargasaurus", "Oviraptor",
"Velociraptor", "Deinonychus", "Dilophosaurus",
"Gallimimus", "Triceratops"
};
DinoDiscoverySet GoMesozoic = new DinoDiscoverySet(dinosaurs);
GoMesozoic.DiscoverAll();
GoMesozoic.DiscoverByEnding("saurus");
}
private string[] dinosaurs;
public DinoDiscoverySet(string[] items)
{
dinosaurs = items;
}
public void DiscoverAll()
{
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
}
public void DiscoverByEnding(string Ending)
{
Predicate<string> dinoType;
switch (Ending.ToLower())
{
case "raptor":
dinoType = EndsWithRaptor;
break;
case "tops":
dinoType = EndsWithTops;
break;
case "saurus":
default:
dinoType = EndsWithSaurus;
break;
}
Console.WriteLine(
"\nArray.Exists(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Exists(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.TrueForAll(dinosaurs, \"{0}\"): {1}",
Ending,
Array.TrueForAll(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.Find(dinosaurs, \"{0}\"): {1}",
Ending,
Array.Find(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.FindLast(dinosaurs, \"{0}\"): {1}",
Ending,
Array.FindLast(dinosaurs, dinoType));
Console.WriteLine(
"\nArray.FindAll(dinosaurs, \"{0}\"):", Ending);
string[] subArray =
Array.FindAll(dinosaurs, dinoType);
foreach(string dinosaur in subArray)
{
Console.WriteLine(dinosaur);
}
}
// Search predicate returns true if a string ends in "saurus".
private bool EndsWithSaurus(string s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "raptor".
private bool EndsWithRaptor(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "raptor"))
{
return true;
}
else
{
return false;
}
}
// Search predicate returns true if a string ends in "tops".
private bool EndsWithTops(String s)
{
if ((s.Length > 3) &&
(s.Substring(s.Length - 4).ToLower() == "tops"))
{
return true;
}
else
{
return false;
}
}
}
输出:
Compsognathus Amargasaurus Oviraptor Velociraptor Deinonychus Dilophosaurus Gallimimus Triceratops Array.Exists(dinosaurs, "saurus"): True Array.TrueForAll(dinosaurs, "saurus"): False Array.Find(dinosaurs, "saurus"): Amargasaurus Array.FindLast(dinosaurs, "saurus"): Dilophosaurus Array.FindAll(dinosaurs, "saurus"): Amargasaurus Dilophosaurus