0

Is there a way to convert the List<T[]> to an array of the same type T[]?

For example: List<class1[]> to Class[]1 ?

I know we can convert List<T> to array T[] using list.Toarray() but here I'm looking for List<T[]> to array T[].

4
  • 3
    I'm guessing Java, but maybe another language? Please tag this question with the language you are using. Commented Jan 15, 2015 at 6:14
  • 1
    Why do you need this for both C# and Java? Commented Jan 15, 2015 at 6:23
  • 1
    So you want to flatten the list? Commented Jan 15, 2015 at 6:24
  • 1
    I need it only for C#, someone added the tag for java Commented Jan 15, 2015 at 6:29

2 Answers 2

6

You appear to be turning a List of Lists into one flat list.

If you are in C#, there are Linq utility functions for this:

// Namespaces you need
using System.Linq;
using System.Collections.Generic;


////////////////////////////////////////////
// In your code block                     //
////////////////////////////////////////////
List<T[]> myJaggedList = new List<T[]>();

// (Populate here)

T[] flatArray = myJaggedList.SelectMany(m => m).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I don't see SelectMany method from list object
@User - You have to include System.Linq as a namespace. I'll update my answer.
2

The Java Class List contains a function List.toArray(). Here is the Documentation.

Edit: There is a generic List.toArray() as well <T> T[] toArray(T[] a) see the same documentation from above.

1 Comment

Maybe you're right, but it seems to me OP has a slightly different question: how to create an array (not an array of arrays) from the list of arrays

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.