Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "List<int>[,]" and "List<List<int>>"

Tags:

c#

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.

like image 947
Only a Curious Mind Avatar asked Jan 11 '23 00:01

Only a Curious Mind


1 Answers

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.

like image 53
Sergey Berezovskiy Avatar answered Jan 22 '23 08:01

Sergey Berezovskiy