I have 3-dimensional array in my programm. I want to debug values in it and I put this array into another array, in which I want to find difference.
I tried this:
Weights weights = new Weights(); // init inside
List<Weights> weightsDebug = new List<Weigts>();
weightsDebug.Add(weights); // I'd put first weigths into the list
for(int i = 0; i < 100; i++) {
// do some bad things with weights;
weightsDebug.Add(weights);
}
After that, I'd got same values in weights in all 99 elements of weigthsDebug. I'd tried to debug and I'd realised, that weigts array is being changed. I understood that problem is in the reference (copy by link, not by value) – all pushed to weightsDebug array elements are linked with weights in main loop.
I'd googled a bit and found some ways how to copy 1-dimensional array. I'tried next:
I added Clone method to my Weights class:
double[][][] values;
public Weights Clone() {
Weights clone = new Weights();
clone.values = (double[][][]) this.values.Clone();
return clone;
}
public Weights()
{
Console.WriteLine("Constructor WITHOUT arguments fired");
}
Now I am doing so when copy weights:
Weights weights = new Weights(); // init inside
List<Weights> weightsDebug = new List<Weigts>();
weightsDebug.Add(weights); // I'd put first weigths into the list
for(int i = 0; i < 100; i++) {
// do some bad things with weights;
Weights weightsClone = weights.Clone();
weightsDebug.Add(weightsClone);
}
And I am still getting last changed value in whole debug array. How I can correct it?