首先,我搜索了这些问题,但没有找到我需要的东西,也许它不存在哈哈,但我会试一试。我是 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
我正尝试在 C# 中执行此操作,但它似乎不起作用;到目前为止我已经试过了:
public class myints
{
public int x { get; set; }
}
public List<myints[]> T = new List<myints[]>();
T[i].Add(new myints() { x = i });
我希望能够添加内容然后使用 Count()
在for
查看我在 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++;
}
您还可以将初始化列表添加到主列表
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 });
关于c# - 多维整数列表 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598657/