1

So,I have three separate lists of integers and I want to convert them in a 2D array. I have tried to concat the 3 lists but it becomes a single array not 2D. I want to take the 1st element of each list and form a 2D array.

var startIndex= new List<int>(){ 0, 0, 0, 0 };
var endIndex=   new List<int>(){ 0, 1, 2, 3};
var subs=  new List<int>(){ 0, 1, 1, 0 };
var queries = startIndex.Concat(endIndex).Concat(subs).ToArray(); //this doesn't work

I want something like this-

var queries = [[0,0,0],[0,1,1],[0,2,1],[0,3,0]] //taking first element from each list and so on

I didn't find an eaasy way in c#, any help would be appreciated?

I have started to convert the lists to arrays-

    var startArr = startIndex.ToArray();
    var endArr = endIndex.ToArray();
    var subArr = subs.ToArray();
  • See [this](https://stackoverflow.com/questions/39484996/rotate-transposing-a-listliststring-using-linq-c-sharp) for how to get a `List>`. You can easily adapt that to produce a `int[][]`. If you want a `int[,]`, I think you'd have to copy the values by yourself. – Sweeper May 03 '21 at 02:37

1 Answers1

1

This will be all you need

        var startIndex = new List<int>() {0, 0, 0, 0};
        var endIndex = new List<int>() {0, 1, 2, 3};
        var subs = new List<int>() {0, 1, 1, 0};
        int maxIndex = startIndex.Count;

        List<List<int>> newList = new List<List<int>>();
        for (int i = 0; i < maxIndex; i++)
        {
            newList.Add(new List<int>() {startIndex[i], endIndex[i], subs[i]});
        }