Whats the difference between List<int>[,]
and List<List<int>>"
in C#?
I know that the call is different and to access the positions too, but the purpose is the same?
I've seen twice implementations that had the same result and that these two forms were implemented.
List<int>[,]
is a two-dimensional array of lists. You should define 'matrix' size which cannot change. After creation you can add lists in cells:
List<int>[,] matrix = new List<int>[2, 3]; // size is fixed
matrix[0, 1] = new List<int>(); // assign list to one of cells
matrix[0, 1].Add(42); // modify list items
List<List<int>>
is a list of lists. You have list which contains other lists as items. It has single dimension, and size of this dimension can vary - you can add or remove inner lists:
List<List<int>> listOfLists = new List<List<int>>(); // size is not fixed
listOfLists.Add(new List<int>()); // add list
listOfLists[0].Add(42); // single dimension
They are different data structures.
Actually you are over-complicating question with items of type List<int>
. Structure will stay same with any type T
. So, you have here two-dimensional array T[,]
and list List<T>
. Which are, as stated above, completely different data structures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With