比方说,我有这样的数据:如何在多个列表交换两个项目
列表:
- ListOfPoints =一
- ListOfPoints = B
- ListOfPoints = C
- ListOfPoints = d
现在我想要做的是:swap每个列表中有两个点(a,b,c,d),不幸的是它不起作用。
我尝试下面的代码:
List<List<Point3d>> currentGeneration = handoverPopulation.ToList();
foreach(List<Point3d> generation in currentGeneration)
{
int index1;
int index2;
Random r = new Random();
index1 = r.Next(0, generation.Count);
index2 = r.Next(0, generation.Count);
if(index1 != index2)
{
Point3d cache = generation[index1];
generation[index1] = generation[index2];
generation[index2] = cache;
}
}
如何在多个列表同时交换两个点或为什么我的方法行不通?
感谢您的帮助。
备注:对于本地变量,“generation”比'Var'更好。您可能想要阅读c#的命名约定。 – user3185569
那是真的,我应该改变局部变量的名字。当我完成基本的程序时,我仍然会在最后改变一些东西。 – Meliss
您的代码遇到了什么问题? – symmetricsaurus