首先,我搜索了一些问题,但我没有找到我需要的东西,也许它不存在哈哈但是我会试一试.我是C#的新手,我来自C++,得到了高中的经验.
在C++中Vector<int> T[];,我可以创建一个大小不知道的列表; 做出这样的事情而不是浪费空间; 更确切地说
T[0][....];
T[1][...];
1 2 3 4 5
1 2 3
2 4 1 5
0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我试图在C#中这样做,它似乎不起作用; 到目前为止我试过这个:
public class myints
{
public int x { get; set; }
}
public List<myints[]> T = new List<myints[]>();
T[i].Add(new myints() { x = i });
Run Code Online (Sandbox Code Playgroud)
我想能够添加东西,然后用Count()在for看我有多少elemts有一个T[i].喜欢T[i].size()......这可能吗?
该程序说System.Array不包含Add的定义
此示例创建一个列表,其中包含许多不同长度的子列表,应该作为您要执行的操作的良好起点.
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++;
}
Run Code Online (Sandbox Code Playgroud)
您还可以将初始化列表添加到主列表中
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 });
Run Code Online (Sandbox Code Playgroud)