User
User

Reputation: 21

How to Convert List<T[]> to array T[]?

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[].

Upvotes: 0

Views: 751

Answers (2)

Andrew Shepherd
Andrew Shepherd

Reputation: 45222

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();

Upvotes: 6

James C. Taylor IV
James C. Taylor IV

Reputation: 609

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.

Upvotes: 2

Related Questions