4

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.

1
  • 1
    I see List<int>[,] as being closer to List<List<List<int>>> (since there's three dimensions in each; the major difference being that the latter is jagged, i.e. each list can be a different size). Commented Mar 14, 2014 at 17:02

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

List<int>[,] is a two-dimensional array of integer Lists.

List<List<int>> is a List of integer Lists.

So they are totally different.The common thing is they both contains integer Lists (List<int>) but one of them is two-dimensional array.Other is a single List of integer Lists.

Comments

0

List<int>[,] is a 2D array of List<int>. That means it has three dimensions (2 of which are fixed). List<List<int>> is a list of lists, so 2 dimensions. So comparing them doesn't make a lot of sense.

A better comparison would be something like List<int>[] vs List<List<int>>. Both are 2D collections of integers, but the first has one dimension fixed while the later can expand in both dimensions.

List<int> A collection of integers which will grow as needed int[] An array of ints that will have a fixed length List<int>[] An array of List<int> which has a fixed number of List<int>, but each List<int> will expand as needed (by adding ints) List<List<int>> A list of list which can grow both by adding more List<int> and by adding ints to one of the List<int> int[,] A 2d array of ints that is fixed in both dimensions and rectangular (i.e. length in both dimensions is always the same) int[][] An array of int[], i.e. a jagged array. Fixed in both dimensions but each int[] can have a different length 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.