I have read through all the answers to similar questions and have tried a bunch of different permutations but nothing seams to work! I've spent the last 4-5h trying to get around I keep getting the same result. Please help!
here is my problematic code: I need to copy the contents of the cruMatrix into cruMatric_tm1 on each iteration. I've left some of the things I've tried commented out
while (true)
{
//cruMatrix = new List<double[]>();
//cruMatrix = cruMatrix_tm1.ToList();
T1 = new Thread(updateCaConcentration); T1.Start(); T1.Join();
T2 = new Thread(updateSystemState); T2.Start(); T2.Join();
//cruMatrix_tm1.Clear();
systemUpdate();
plotOpenVsTime(ref time);
//cruMatrix_tm1 = new List<double[]>(cruMatrix);
cruMatrix_tm1 = new List<double[]>();
foreach(double[] arr in cruMatrix){
cruMatrix_tm1.Add(arr);
}
run++;
if (run > runUntil) break;
time++;
}
double[] is a reference type. When you add the array from one list to another you adding a reference to the same array. Any modifications made to the array will be reflected in both lists. What you want to do is create a new array and use something like Array.Copy like this:
cruMatrix_tm1 = new List<double[]>();
foreach(double[] arr in cruMatrix){
double[] copy = new double[arr.Length];
Array.Copy(arr,copy,arr.Length);
cruMatrix_tm1.Add(copy);
}
You keep creating a new List for cruMatrix_tm1 inside the looping, that's why cruMatrix_tm1 always have the same element.
//put it outside the loop
cruMatrix_tm1 = new List<double[]>();
while (true)
{
T1 = new Thread(updateCaConcentration); T1.Start(); T1.Join();
T2 = new Thread(updateSystemState); T2.Start(); T2.Join();
systemUpdate();
plotOpenVsTime(ref time);
foreach(double[] arr in cruMatrix){
cruMatrix_tm1.Add(arr);
}
run++;
if (run > runUntil) break;
time++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.