5

First of all i searched through the questions and I haven't found the thing I need , maybe it doesn't exist haha but i'll give it a shot.I am new to C# and I am coming from C++, got highschool experience.

In C++ I had Vector<int> T[]; so I could create a list with a size that it wasn't know; and make something like this and not wasting space; to be more exact

T[0][....];
T[1][...];

1 2 3 4 5
1 2 3 
2 4 1 5
0 0 0 0 0 0

I am trying to do this in C# and It doesn't seem to work; I have tried this so far:

 public class myints
    {
        public int x { get; set; }
    }

  public List<myints[]> T = new List<myints[]>();

  T[i].Add(new myints() { x = i });

I wanna be able to add stuff and then use Count() in a for to see how many elemts I have in a T[i]. like T[i].size()... Is this possible?

the program says System.Array does not contain a definition for Add

Hogan
  • 69,564
  • 10
  • 76
  • 117
rakuens
  • 176
  • 1
  • 7
  • 2
    Arrays are of fixed-size, just like in C++ BTW. Use `List>`. – Lucas Trzesniewski Jan 04 '16 at 19:53
  • List as Lucas Trzesniewski suggested above is what you are looking for. But your comment about no space being wasted is not correct. List is like a resizing array. When .Add is called on a full List, the framework doubles the allocated size, and I believe never shrinks it (I'd have to check the .NET source to be sure). When it is no longer referenced however, it will of course be garbage collected. – Clay Ver Valen Jan 04 '16 at 20:08
  • This has a nice explanation http://stackoverflow.com/questions/665299/are-2-dimensional-lists-possible-in-c – dashnick Jan 04 '16 at 20:10
  • @ClayVerValen it never does shrink it by itself since it would be [wasteful to do so](http://stackoverflow.com/a/34233735/3764814), but you can set the `Capacity` property yourself if you *really know* it's a good idea in a particular case. – Lucas Trzesniewski Jan 04 '16 at 20:12
  • @Lucas Trzesniewski I went with - public List> T = new List>(); - and using T[i].Add(j); under 2 fors, to add stuff and I'm getting a out of range() out of index?? error when I'm doing this at the T[i].Add(j); ... what do I do? – rakuens Jan 04 '16 at 20:23
  • @rakuens use the debugger :) `i` is out of bounds for `T`, like the error says. – Lucas Trzesniewski Jan 04 '16 at 20:24
  • @Lucas - Agreed. That is what I had read, but often sources on the web are incorrect about such matters, so without taking the time to look at the .NET source, I did not care to state it definitively. It was only the OP's statement regarding "... not wasting space ..." that caused my concern as it showed a poor understanding of the algorithm(s) used behind resizing arrays (in other languages) or List (in .NET) work. – Clay Ver Valen Jan 04 '16 at 20:25

1 Answers1

5

This example creates a list with a number of sublists of varying length and should serve as a good starting point for what you want to do.

List<List<int>> mainlist = new List<List<int>>();
List<int> counter = new List<int>() { 5, 4, 7, 2 };
int j = 0;

// Fill sublists
foreach(int c in counter)
{
    mainlist.Add(new List<int>(c));
    for(int i = 0; i < c; i++ )
        mainlist[j].Add(i);
    j++;
 }

You could also add initialized lists to the main list

List<List<int>> mainlist = new List<List<int>>();
mainlist.Add(new List<int>() { 1, 5, 7 });
mainlist.Add(new List<int>() { 0, 2, 4, 6, 8 });
mainlist.Add(new List<int>() { 0, 0, 0 });
Yurrit Avonds
  • 294
  • 3
  • 13
  • Can I do this without knowing the length? what I'm doing is reading nodes and adding them slowly to the list , this example gave me a better understanding though. Why do I have to add a 3rd list to the list? if there are already 2 declared – rakuens Jan 04 '16 at 20:36
  • yes, these Lists dynamically grow as you add things, welcome to the nice world of c# – pm100 Jan 04 '16 at 20:50
  • You do not 'have to' add a 3rd list. This is just an example. You can add as many lists as you like. – Yurrit Avonds Jan 04 '16 at 20:53