Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何增加列表名?_C#_Loops_Increment - Fatal编程技术网

C# 如何增加列表名?

C# 如何增加列表名?,c#,loops,increment,C#,Loops,Increment,我将首先向您展示我的代码,因为我很难找到文字来解释我想要什么,我有一个点列表,其中包含坐标列表点,精确到20个坐标。我想把这20点分成4个点。以下是我的4点列表: List<PointF> Col1 = new List<PointF>(); List<PointF> Col2 = new List<PointF>(); List<PointF> Col3 = new List<Po

我将首先向您展示我的代码,因为我很难找到文字来解释我想要什么,我有一个点列表,其中包含坐标
列表点,精确到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>();
List Col1=新列表();
List Col2=新列表();
List Col3=新列表();
List Col4=新列表();
这是我的代码:

        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));   
            }
        }
int循环=1;

while(loop您可以使用
字典
执行此操作,如下所示:

var result=newdictionary();
int标识符=0;
结果[标识符]=新列表();
for(int-loop=0;loop<19;loop++)
{
如果((循环-1)%5==0)
{
结果[++标识符]=新列表();
}
结果[identifier]。添加(点[loop]);
}

请参阅以获取替代方法。

您可以使用
字典来执行此操作,如下所示:

var result=newdictionary();
int标识符=0;
结果[标识符]=新列表();
for(int-loop=0;loop<19;loop++)
{
如果((循环-1)%5==0)
{
结果[++标识符]=新列表();
}
结果[identifier]。添加(点[loop]);
}

请参阅以获取替代方案。

阵列可能是更好的解决方案:

List<PointF>[] lists = {new List<PointF>(), new List<PointF>(), new List<PointF>(), new List<PointF>()};

for(int i=0;i<20;i++) lists[i/5].Add(points[i]);
List[]List={new List(),new List(),new List(),new List()};

对于(int i=0;i而言,数组可能是更好的解决方案:

List<PointF>[] lists = {new List<PointF>(), new List<PointF>(), new List<PointF>(), new List<PointF>()};

for(int i=0;i<20;i++) lists[i/5].Add(points[i]);
List[]List={new List(),new List(),new List(),new List()};

对于(int i=0;iYou可以使用字典。MSDN参考:感谢您的快速回复,但是,有没有像我在问题中给出的最后一个代码那样更简单的方法?它看起来很复杂,这正是@AdrianFaciu的答案所要做的。您需要将您的4个列表放入另一个集合中,以便您可以轻松地在它们之间切换。我得到了它知道是因为@codeCaster制作的示例代码,但我仍然感谢你们!你们可以使用字典。MSDN参考:谢谢你们的快速回复,但是,有没有像我在问题中给出的第二个最后代码那样更简单的方法?它似乎太复杂了@AdrianFaciu的答案正是如此。你需要把你的4个列表到另一个集合中,以便您可以轻松地在它们之间切换。我之所以知道这一点,是因为制作了@codeCaster示例代码,但我仍然感谢你们!如果我想对我的第一个结果进行排序,是否正确?结果[1]。OrderBy(c=>c.Y)。ToList();我知道这是最好的方法,但我在c#中使用这种方法时遇到了困难。我只处理了20个点,所以我想如果我使用@ic3b3rg的方法是可以的,这对我来说更容易理解。但是无论如何,非常感谢!如果我想对我的第一个结果排序,这是正确的吗?结果[1]。OrderBy(c=>c.Y)。ToList();我知道这是最好的方法,但我在c#中使用这种方法时遇到了困难。我只处理了20分,所以我想如果我使用@ic3b3rg的方法是可以的,这对我来说更容易理解。但无论如何,谢谢!
List<PointF>[] lists = {new List<PointF>(), new List<PointF>(), new List<PointF>(), new List<PointF>()};

for(int i=0;i<20;i++) lists[i/5].Add(points[i]);