0

How to set size to list of lists of objects ? I dont know it until I run the program. I want to set it to m - rows, n - columns, depends on users input.

public class MyCLass
{
//...
}

List<List<MyClass>> exampleList = new List<List<MyClass>>();

something like this..maybe ? only in 2 dimensions

[pic][1]
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
makima89
  • 15
  • 6
  • Set the size of what? A data grid of some kind? – John Wu Dec 13 '17 at 02:22
  • aha sorry. size of the matrix, where each field is object – makima89 Dec 13 '17 at 02:22
  • Can you clarify your question please? Are you looking for something like this? [How to convert jagged array to 2D array?](https://stackoverflow.com/q/20614694)? There's also [How to convert list of arrays into a multidimensional array](https://stackoverflow.com/q/9774901). – dbc Dec 13 '17 at 02:25
  • I think no, I also found that before I posted question. I cant use arrays as I dont know the size of a matrix because user should choose it during runtime. I need matrix size m,n, which will have Objects for each field (1,1 etc.). I am quite sure that I need to use List of lists just I dont know how to set its size after user enter it. – makima89 Dec 13 '17 at 02:30
  • What do you mean size? size of length of an object?You need a max length? – Vijunav Vastivch Dec 13 '17 at 02:39
  • 1
    You can keep adding items to the end of a list and it will keep expanding. If you want to add a bunch of items then you can use `AddRange()`. If you want to initialize a list to a bunch of zeros then you can do this: `mylist.AddRange(Enumerable.Range(0, count))` – Ray Fischer Dec 13 '17 at 02:40
  • Try to add more sample details here and give a sample result. – Vijunav Vastivch Dec 13 '17 at 02:41
  • not the object. size of list of lists..each row is list of same size, that creates a matrix..I wonder how to set size of that matrix...doesnt matter the object, I just mentioned that matrix on each field contains an object. – makima89 Dec 13 '17 at 02:42

1 Answers1

0

If you have two variables populated with values from the user, m and n, and you want a matrix of m x n storage locations, you would simply use:

var matrix = new MyClass[m][n];

For a matrix, you would not use a List or List<List>, as a matrix has a fixed size and items in the matrix stay in the location to which they were assigned, while lists are designed to grow and shrink as you add or remove objects, and items in the list(s) can change position as a result.

John Wu
  • 50,556
  • 8
  • 44
  • 80