Finding Array Elements Example

One main reason of storing information in an array is having the ability to search for items in the array. In this example you will see the use of array methods IndexOf, FindIndex, and FindAll.

FindingElementsProgram FindingElementsSolution

 
Program.cs code contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindingElements
{
    class Program
    {
        /// <summary>
        /// Demonstrating different ways to find array elements.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

            // Finding a specific element by name.
            findSpecificElement(daysOfWeek);

            // Finding an element by a letter.
            findElementByLetter(daysOfWeek);

            // Finding all of the specified elements.
            findAllElements(daysOfWeek);

            Console.ReadLine();
        }

        private static void findSpecificElement(string[] daysOfWeek)
        {
            // Finding a specific element in the array.
            Console.WriteLine("****Finding A Day Of Week Element By Name****");

            // You could also use LastIndexOf().
            int index = Array.IndexOf(daysOfWeek, "Tuesday");
            Console.WriteLine("Array Index: {0}", index);
            Console.WriteLine("{0}", daysOfWeek[index]);
        }

        private static void findElementByLetter(string[] daysOfWeek)
        {
            // Another way to find a specific array element by letter.
            Console.WriteLine("****Finding A Day Of Week By Letter****");

            // You could also use FindLastIndex().
            int index = Array.FindIndex(daysOfWeek, x => x[0] == 'W');
            Console.WriteLine("Array Index: {0}", index);
            Console.WriteLine(daysOfWeek[index]);
        }

        private static void findAllElements(string[] daysOfWeek)
        {
            // Finding all of the specified elements. To find different elements change the value of the 'value' variable.
            string value = "M";
            string[] daysThatContain = Array.FindAll(daysOfWeek, x => x.Contains(value));

            Console.WriteLine("****Days That Contain Value Of {0}", value);
            foreach (string item in daysThatContain)
            {
                Console.WriteLine(item);
            }
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s