0

I have this following array in C#:

string[][] arrayofArrays = getArray();

I know that the arrays which are in arrayofArrays have a length of 2. My question is the following: is it possible to "split" or to "separate" the principal array in two arrays?

Thanks.

EDIT: I would like to know if there is a way which is already implemented in C#, I know how to do that with an algorithm.

EDIT 2: here is an example of what I need:

// here are the data:
string[][] arrayofArrays = {{"1", "a"}, {"2", "b"}, {"3", "c"}};

//here is what I need as output:
string[] array1 = {"1", "2", "3"};
string[] array2 = {"a", "b", "c"};
5
  • As a declaration, or as a procedure/algorithm? Commented Jan 12, 2015 at 13:14
  • Split it how? Based on what criteria? Commented Jan 12, 2015 at 13:14
  • You have a 2D array and you want to separate it into 2 single dimension array ? Commented Jan 12, 2015 at 13:15
  • I would like to know if there is a way which is already implemented in C#, I know how to do that with an algorithm. Commented Jan 12, 2015 at 13:15
  • 2
    Please could you provide examples of the data returned by the getArray() method and the corresponding expected content of the arrayOfArrays object? Commented Jan 12, 2015 at 13:18

3 Answers 3

5

If I understand you right, you can do this:

string[] firstArray=Array.ConvertAll(arrayofArrays,a => a[0]);
string[] secondArray=Array.ConvertAll(arrayofArrays,a => a[1]);
Sign up to request clarification or add additional context in comments.

Comments

3

And one more variant:

var array1 = arrayOfArrays.Select(a => a[0]).ToArray();
var array2 = arrayOfArrays.Select(a => a[1]).ToArray();

Comments

1

For what it's worth, this LINQ query should do what you want:

int numArrays = 2;
string[][] arrayofTwoArrays = arrayofArrays
    .Select((arr, index) => new { arr, index })
    .GroupBy(x => (x.index + 1) % numArrays, x => x.arr)
    .Select(g => g.SelectMany(x => x).ToArray())
    .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.