3

I am converting a list within a list to an int[][] using:

int[][] preConvertInts = processedList.Select(array => array.ToArray().ToArray()) as int[][];

I am returning an int[,] in my method. What is the best way to convert an int[][] to an int[,]?

Edit: method

public static int[,] GetIntArrayInts(string dataString){
    string data = dataString;
    data = data.Replace(Environment.NewLine, "\n");

    List<List<int>> processedList = new List<List<int>>();

    string[] rows = data.Split('\n');
    foreach (string row in rows){
        string[] columns = row.Split(',');
        List<int> ints = new List<int>();
        foreach (string column in columns){
            if (int.TryParse(column, out int tileGid)) ints.Add(tileGid);
        }
        processedList.Add(ints);
    }
    int[][] preConvertInts = processedList.Select(array => array.ToArray().ToArray()) as int[][];
    int[,] processedIntArray =  new int[preConvertInts[0].Length, preConvertInts[1].Length];
    for (int i = 0; i < preConvertInts.Length; i++){
        int[] intArray = preConvertInts[i];
        for (int j = 0; j < intArray.Length; j++){
            processedIntArray[i, j] = preConvertInts[i][j];
        }
    }
    return processedIntArray;
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Luna
  • 73
  • 1
  • 6

2 Answers2

5

What is the best way to convert an int[][] to an int[,]?

it is the one that is described in this post. Yours will actually also work if all sub-arrays have the same Length. But to cite you:

Unfortunately, my array is not rectangular. – Luna

Then it would not make much sense to try to convert it. Unless you loose values or you add values to make the dimensions of all sub arrays equal.

But your problem in the code is not this conversion but this one:

I am converting a list within a list to an int[][] using:
int[][] preConvertInts = processedList.Select(array => array.ToArray().ToArray()) as int[][];

This is wrong. You will get null for preConvertInts! if you check the return value of the Select call:

enter image description here

You can see that it returns IEnumerable<int[]> and not int[][]. Casting it with as int[][]; does not work and masks only the fact that the 2 types are different from the compiler. What you do there is to convert each sublist into an array and then convert (the already converted array) simply again into an array.

You need to make the select in the proper way:

int [][] preConvertInts = processedList.Select(x=>x.ToArray()).ToArray();

Explanation:

1) in the first step you collect all sublists and convert each one into an array: Select(x=>x.ToArray())

2) now this call returns an IEnumerable<int[]> which you need to convert again to an array:

Select(x=>x.ToArray()).ToArray();
                     ^^
                     ||
    note the dot behind the closing parentesis of the Select call
Luna
  • 73
  • 1
  • 6
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
2

Jagged Array

public static T[,] ToMultiArray<T>(this IList<T[]> arrays)
{
    var length = arrays[0].Length;
    var result = new T[arrays.Count, length];
    for (var i = 0; i < arrays.Count; i++)
    {
        var array = arrays[i];
        if (array.Length != length)
        {
            throw new ArgumentException("Misaligned arrays");
        }

        for (var j = 0; j < length; j++)
        {
            result[i, j] = array[j];
        }
    }

    return result;
}

Multidimensional Array

public static T[][] ToJaggedArray<T>(this IList<T[]> arrays)
{

    var result = new T[arrays.Count][];
    for (var i = 0; i < arrays.Count; i++)
    {
        var array = arrays[i];
        var length = array.Length;
        result[i] = new T[length];
        for (var j = 0; j < length; j++)
        {
            result[i][j] = array[j];
        }
    }

    return result;
}

Usage

var preConvertInts = list.ToJaggedArray();

or

var preConvertInts = list.ToMultiArray();

Update

this IList<T[]> arrays this method is for lists which contain arrays, to fit OP's example it should be (this IList<List<T>> arrays) – Mong Zhu

Jagged Array

public static T[][] ToJaggedArray<T>(IList<List<T>> arrays)
{

    var result = new T[arrays.Count][];
    for (var i = 0; i < arrays.Count; i++)
    {
        var array = arrays[i];
        var length = array.Count;
        result[i] = new T[length];
        for (var j = 0; j < length; j++)
        {
            result[i][j] = array[j];
        }
    }

    return result;
}

Multidimensional Array

public static T[,] ToMultiArray<T>(IList<List<T>> arrays)
{
    var length = arrays[0].Count;
    var result = new T[arrays.Count, length];
    for (var i = 0; i < arrays.Count; i++)
    {
        var array = arrays[i];
        if (array.Count != length)
        {
            throw new ArgumentException("Misaligned arrays");
        }

        for (var j = 0; j < length; j++)
        {
            result[i, j] = array[j];
        }
    }

    return result;
}

Note : Totally untested

TheGeneral
  • 79,002
  • 9
  • 103
  • 141