Posted by: Cirilo Meggiolaro | 03/9/2009

Tip of the day #146 – Find functions for arrays and generic lists

Arrays and generic lists have several functions to help you find and retrieve an item or its index or a series of items that match a specified Predicate.

Method signature for arrays (under Array class)

public static T Find<T>(T[] array, Predicate<T> match);
public static T[] FindAll<T>(T[] array, Predicate<T> match);
public static int FindIndex<T>(T[] array, Predicate<T> match);
public static int FindIndex<T>(T[] array, int startIndex, Predicate<T> match);
public static int FindIndex<T>(T[] array, int startIndex, int count, Predicate<T> match);
public static T FindLast<T>(T[] array, Predicate<T> match);
public static int FindLastIndex<T>(T[] array, Predicate<T> match);
public static int FindLastIndex<T>(T[] array, int startIndex, Predicate<T> match);
public static int FindLastIndex<T>(T[] array, int startIndex, int count, Predicate<T> match);

Example

Assuming we have an array that stores strings containing city and country names let’s use the FindAll function to retrieve all cities from USA:

string[] cities = { “New York, USA”, “Los Angeles, USA”,
                          “Miami, USA”, “Toronto, Canada”,
                          “Boston, USA”, “Vancouver, Canada” };

foreach (string americanCity in Array.FindAll(cities, s => s.EndsWith(“USA”)))
{
    Console.WriteLine(americanCity);
}

Output:
New York, USA
Los Angeles, USA
Miami, USA
Boston, USA

Method signature for generic lists

public T Find(Predicate<T> match);
public List<T> FindAll(Predicate<T> match);
public int FindIndex(Predicate<T> match);
public int FindIndex(int startIndex, Predicate<T> match);
public int FindIndex(int startIndex, int count, Predicate<T> match);
public T FindLast(Predicate<T> match);
public int FindLastIndex(Predicate<T> match);
public int FindLastIndex(int startIndex, Predicate<T> match);
public int FindLastIndex(int startIndex, int count, Predicate<T> match);

Example

For this example, let’s use the FindIndex function to get the indexes of all items that contain Canadian cities:

List<string> cities = new List<string>();
cities.Add(“New York, USA”);
cities.Add(“Los Angeles, USA”);
cities.Add(“Miami, USA”);
cities.Add(“Toronto, Canada”);
cities.Add(“Boston, USA”);
cities.Add(“Vancouver, Canada”);

int index = -1;

while (true)
{
    index = cities.FindIndex(index + 1, s => s.EndsWith(“Canada”));

    if (index >= 0)
        Console.WriteLine(“Index: {0}”, index);
    else
        break;
}

Output:
Index: 3
Index: 5


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: