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}?

martijn
  • 1,417
  • 1
  • 16
  • 26

4 Answers4

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();
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
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();
Eser
  • 12,346
  • 1
  • 22
  • 32
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());
        }
  • why not use array directly instead calling `ToArray` on list? you already know length, that not changed. – Grundy Oct 02 '15 at 09:32
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();
Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • 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. ;) – Rob May 26 '23 at 04:31