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;
}
6
  • 3
    int[][] is Jagged array Commented Feb 15, 2018 at 11:20
  • I suppose best way would be to just use a for loop(s). Commented Feb 15, 2018 at 11:21
  • @mjwills I'm not sure what you're referring to since there is no problem I'm running into. Commented Feb 15, 2018 at 11:24
  • @FaizanRabbani I was in the process of adding the method. There you go. Commented Feb 15, 2018 at 11:27
  • Thanks, deleted my comment. Commented Feb 15, 2018 at 11:27

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

7 Comments

Nice, up vote because im eating icecream and its awesome
@MichaelRandall Bon appétit
@MichaelRandall I added a freehand drawn circle and arrow to the image to make it even more awesome ;)
If i could vote up that arrow i would
This helped. I realised my mistake just a little later.
|
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

12 Comments

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)
@MongZhu thanks for the heads up
otherwise your method works. But so does the one in the duplicate :)
@MongZhu i have no idea!
@m.rogalski i guess in this example the multi version expects the same length, but you raise a good points, it could just creat the biggest occurrence and default up the type for anything missing
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.