【发布时间】:2014-02-10 15:02:24
【问题描述】:
我将首先向您展示我的代码,因为我很难找到词语来解释我想要什么,我有一个包含坐标List<PointF> points; 的点列表,确切地说是 20 个坐标。我想将这 20 个点分成 4 个点列表。这是我的 4 点列表:
List<PointF> Col1 = new List<PointF>();
List<PointF> Col2 = new List<PointF>();
List<PointF> Col3 = new List<PointF>();
List<PointF> Col4 = new List<PointF>();
这是我的代码:
int loop = 1;
while (loop <= 20)
{
var identifier = 1;
if (loop % 5 == 0)
{
identifier++;
}
else
{//Here is what I'm talking about, if I want it to be like Col1 + identifier.ToString(), something like that
Col.Add(new PointF(points.ElementAt(loop - 1).X, points.ElementAt(loop - 1).Y));
}
}
我想要做的是,如果我的循环已经是 5,我希望我的 Col1.add 为“Col2.add”,如果我的循环等于 10,我希望我的 >Col2.add 为“Col3.add”,如果我的循环等于 15,我希望我的 Col3.add 为“Col4.add”。我不知道怎么说,但我想增加我的列表名称。
我想要这样的东西,但它带有 20 PictureBox 而不是变量。
for (int x = 1; x <= ExtractedBoxes.Count(); x++)
{
((PictureBox)this.Controls["pictureBox" + x.ToString()]).Image = ExtractedBoxes[x - 1];
}
【问题讨论】:
-
您可以使用 Dictionary
>。 MSDN 参考:msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx -
感谢您的快速回复,但是,有没有像我在问题中给出的倒数第二个代码那样更简单的方法?好像很复杂
-
这正是@AdrianFaciu 的答案。您需要将 4 个列表放入另一个集合中,以便在它们之间轻松切换。
-
通过@codeCaster 制作的示例代码我知道了,但我仍然感谢你们!