0

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++;
        }
Jesse Myers
  • 69
  • 1
  • 8
  • May be this article [deep cloning objects](http://stackoverflow.com/questions/78536/deep-cloning-objects) may help – Patrik Jul 24 '15 at 06:30
  • Are you sure that `List` is really the type you want to use? What are you doing? Please provide more information – poke Jul 24 '15 at 06:50
  • I'm using this to store information about a set of coordinates, a state variable and one more piece of information so each entire looks like this {x,y, state, level} – Jesse Myers Jul 24 '15 at 07:35

2 Answers2

0

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);
}
Kevin
  • 2,281
  • 1
  • 14
  • 16
  • this looks promising I'll try it when I get back to the lab tomorrow. Thank you! I was hoping that there is a way to do it without the foreach loop from efficiency considerations since those lists are very long on the order of 1000-10000 entries. Also if this works I'll need to turn **bold**(cruMatrix_tm1) into a `code'(List>) in order to store the progress of the system during the loop iterations. – Jesse Myers Jul 24 '15 at 07:42
0

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++;
        }
currarpickt
  • 2,290
  • 4
  • 24
  • 39