4

I have a multi dimensional array which i need to convert to a list of arrays. Not one single array, but for each iteration of the first dimension i need a separate array containing the values in the second dimension.

How do I convert this:

int[,] dummyArray = new int[,] { {1,2,3}, {4,5,6}};

into a list<int[]> holding two arrays with values {1,2,3} and {4,5,6}?

3
  • 3
    The keyword for this is "flatten" for online searches. Commented Oct 2, 2015 at 7:29
  • 1
    @BananaAcid, but OP not want List<int> he want List<int[]> Commented Oct 2, 2015 at 7:31
  • @Grundy: right, but how "flat" it should be - i believe, example code could be adjusted. Like the LINQ example link below, just takes the first "level" out of it. Commented Oct 2, 2015 at 7:33

4 Answers 4

4

You can convert 2d array into jagged array and then convert it to List.

int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

int[][] jagged = new int[arr.GetLength(0)][];

for (int i = 0; i < arr.GetLength(0); i++)
{
    jagged[i] = new int[arr.GetLength(1)];
    for (int j = 0; j < arr.GetLength(1); j++)
    {
        jagged[i][j] = arr[i, j];
    }
}

List<int[]> list = jagged.ToList();
Sign up to request clarification or add additional context in comments.

Comments

4

You can use Linq:

int[,] dummyArray = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int count = 0;
List<int[]> list = dummyArray.Cast<int>()
                    .GroupBy(x => count++ / dummyArray.GetLength(1))
                    .Select(g => g.ToArray())
                    .ToList();

Comments

1

You could use for loop like this:

        int[,] dummyArray = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

        int size1 = dummyArray.GetLength(1);
        int size0 = dummyArray.GetLength(0);

        List<int[]> list = new List<int[]>();

        for (int i = 0; i < size0; i++)
        {
            List<int> newList = new List<int>();

            for (int j = 0; j < size1; j++)
            {
                newList.Add(dummyArray[i, j]);
            }

            list.Add(newList.ToArray());
        }

1 Comment

why not use array directly instead calling ToArray on list? you already know length, that not changed.
1

Here is a reusable implementation

public static class Utils
{
    public static List<T[]> To1DArrayList<T>(this T[,] source)
    {
        if (source == null) throw new ArgumentNullException("source");
        int rowCount = source.GetLength(0), colCount = source.GetLength(1);
        var list = new List<T[]>(rowCount);
        for (int row = 0; row < rowCount; row++)
        {
            var data = new T[colCount];
            for (int col = 0; col < data.Length; col++)
                data[col] = source[row, col];
            list.Add(data);
        }
        return list;
    }
}

and sample usage

var source = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
var result = source.To1DArrayList();

Some comments on other answers.

M.kazem Akhgary: If I need a list, I don't see why should I first create jagged array and convert it to a list instead of creating list directly.

Eser: I usually like his elegant Linq solutions, but this definitely is not one of them. If the idea is to use Linq (although I strongly believe it's not intended for that), the following would be much more appropriate:

var source = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
var result = Enumerable.Range(0, source.GetLength(0))
    .Select(row => Enumerable.Range(0, source.GetLength(1))
    .Select(col => source[row, col]).ToArray())
    .ToList();

1 Comment

Yours should be the accepted answer, and I agree with your comments on other answers. I read those first (since they appear above yours) and my steam level was increasing until I finally saw your 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.