6

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(???)
RoastBeast
  • 1,059
  • 2
  • 22
  • 38

3 Answers3

12

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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

Community
  • 1
  • 1
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • Micro-benchmarks like that are notoriously hard to get right. The apparent 11 times difference, instead of the more typical 3 times difference, comes from two sources: (1) your tests are apples-to-oranges, because the `FOR` test never resests the list, and (2) both tests do "cold" measurements, meaning that the just-in-time compiler timing is counted with the timing of the code itself. [Here](http://ideone.com/RVRbPl) is your modified benchmark, showing 1689 vs. 535 tick difference. – Sergey Kalinichenko Jul 14 '15 at 20:37
1

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

foreach( Array arItems in listOfArrays )
    myListOfStrings.Add( arItem[ 1 ] );
Neil
  • 1,036
  • 9
  • 18
  • 1
    But then again who doesn't want to use linq? – maraaaaaaaa Jul 14 '15 at 20:02
  • Well, for speed etc - I do not just use LINQ for simple jobs like this. I now note the times post above - I wonder why I have been marked down ... Hmmm! – Neil Jul 14 '15 at 20:43