RoastBeast
RoastBeast

Reputation: 1115

C# - How to extract a list of one particular array member from a list of arrays

Given a list of arrays, I'd like to extract the second member of each array in the list to a list of strings.

For example, say the list of arrays contains: {"1", "A", "X"} {"2", "B", "Y"} {"3", "C", "Z"}

then what I'd like the output to be is a list of strings containing: "A", "B" & "C"

so that I could say:

List<string> myListOfStrings = new List<string>();

myListofStrings.AddRange(???)

Upvotes: 6

Views: 2862

Answers (3)

Fabjan
Fabjan

Reputation: 13676

You can just create new array and put elements that you need from your jagged array to it:

List<string> myListOfStrings  = (new[] {listOfArrays[0][1], listOfArrays[1][1]), listOfArrays[2][1]}).ToList();

Or do something like:

List<string> myListOfStrings = new List<string>();

for(int i = 0; i < listOfArrays.Length; i++) myListOfStrings.Add(listOfArrays[i][1]);

P.S. Specially for dasblinkenligh :

class Program
{
    static string[][] GetRandomJaggedArray(int numberOfArrays, int numberOfElements)
    {
        string[][] temp = new string[numberOfArrays][];
        for (int i = 0; i < numberOfArrays; i++)
        {
           temp[i] = new string[numberOfElements];
           for (int i2 = 0; i2 < temp.Length; i2++)
               temp[i][i2] = Guid.NewGuid().ToString();                                                                                              
        }

        return temp;
    }

    static TimeSpan getElementsAtIndexLINQ(string[][] listOfArrays, int index, int count)
    {
        Stopwatch s  = new Stopwatch(); 
        List<string> myListOfStrings;

        s.Start();
        for (int i = 0; i < count; i++)            
            myListOfStrings = listOfArrays.Select(a => a[index]).ToList();            
        s.Stop();
        return s.Elapsed;
    }

    static TimeSpan getElementsAtIndexFOR(string[][] listOfArrays, int index, int count)
    {
        Stopwatch s = new Stopwatch();
        List<string> myListOfStrings = new List<string>();

        s.Start();
        for (int i2 = 0; i2 < count; i2++ )
            for (int i = 0; i < listOfArrays.Length; i++) myListOfStrings.Add(listOfArrays[i][index]);
        s.Stop();
        return s.Elapsed;
    }

    static void Main(string[] args)
    {
        string[][] test1 = GetRandomJaggedArray(100, 1000);

        string[][] test2 = GetRandomJaggedArray(100, 1000);

        TimeSpan t1 = getElementsAtIndexLINQ(test1, 1, 10);

        TimeSpan t2 = getElementsAtIndexFOR(test2, 1, 10);

        Console.WriteLine("Linq method took {0} ticks to execute", t1.Ticks);

        Console.WriteLine("For method took {0} ticks to execute", t2.Ticks);

        Console.ReadKey();
    }
}

Output :

enter image description here

More about LINQ vs FOR here : For vs. Linq - Performance vs. Future

Upvotes: 2

Neil
Neil

Reputation: 1066

If you didn't want to use LINQ - try ...

foreach( Array arItems in listOfArrays )
    myListOfStrings.Add( arItem[ 1 ] );

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

If you know for sure that all arrays have element at index 1, you could do it with LINQ in a single line:

var myListOfStrings = listOfArrays.Select(a => a[1]).ToList();

Demo.

Upvotes: 12

Related Questions