0

MultiDimensional 2D int array to string List


I would like to convert my array2D:

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

to a List:

List<String> numbers = new List<string>(array2d.ToString());

2 Answers 2

5

You can flatten a multi-dimensional array with Enumerable.Cast

List<String> number = array2D.Cast<int>().Select(i => i.ToString()).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.