我有一个对象Array和一个特定的List。 此对象数组中的每个对象应分别对此字符串起作用并返回一个数字:
{
var nT = 0;
var nF = 0;
#region Irisdataset
Irisdataset[] dataobj = new Irisdataset[150];
#endregion
foreach (var data in dataobj)
{
bool ChroValue = false;
List<List<string>> localchro = new List<List<string>>(chromosome);
ChroValue = ExpValueForEachData(data, localchro);
if (ChroValue == true)
nT++;
else
{
nF++;
}
}
return 1.1
}
我创建了染色体的局部复制,然后将其传递给函数,但它没有用。并且,在执行“ExpValueForEachData”一次后,染色体发生了变化。 我该怎么办?
答案 0 :(得分:1)
在这种情况下,您正在做一个称为浅拷贝的内容,您想要进行深层复制。
尝试以下方法:
List<List<string>> localchro = new List<List<string>>();
foreach(List<string> list in chromosome)
{
// chromosome is holding references to the address of a List<string>
// You want to create a new copy of the data within the reference
// This is a deep copy
localchro.Add(new List<string>(list));
}