Yaser
Yaser

Reputation: 53

Why list members change unwantedly in C#?

I have a problem with my C# program. I want to allocate some values to a 3*3 matrix that is a two-dimensional list in my C# code. This is a part of my code:

        List<double> coefs = new List<double>();
        List<List<double>> a = new List<List<double>>();
        List<double> b = new List<double>();
        List<double> u3 = new List<double>();
        List<double> x2 = new List<double>();
        List<double> x3 = new List<double>();
        List<double> x4 = new List<double>();
        List<double> xy = new List<double>();
        List<double> x2y = new List<double>();


        for (int i=0;i<3;++i)
            u3.add(0);

        for (int i = 0; i < 3; ++i)
        {
            a.Add(u3);
            b.Add(0);
        }

            for (int i = 0; i < n; ++i)
            {
                x2.Add(Math.Pow(x[i], 2));
                x3.Add(Math.Pow(x[i], 3));
                x4.Add(Math.Pow(x[i], 4));
                xy.Add(x[i] * y[i]);
                x2y.Add(Math.Pow(x[i], 2) * y[i]);
            }
        a[0][0] = n;
        a[0][1] = sum(x, n);
        a[0][2] = sum(x2, n);
        a[1][0] = a[0][1];
        a[1][1] = a[0][2];
        a[1][2] = sum(x3, n);
        a[2][0] = a[0][2];
        a[2][1] = a[1][2];
        a[2][2] = sum(x4, n);
        b[0] = sum(y, n);
        b[1] = sum(xy, n);
        b[2] = sum(x2y, n);

x and y are lists of data. At the end of my code, I calculate and allocate a[0][0], a[0][1] and a[0][2]. But ate next command, when the program reaches to a[1][0], a[1][1] and a[1][2] and ..., old values including a[0][0], a[0][1] and a[0][2] are changing automatically. What is the problem and how can I fix it?

Thank you.

Upvotes: 0

Views: 73

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

The problem is this

    List<double> u3 = new List<double>();

    for (int i=0;i<3;++i)
        u3.add(0);

    for (int i = 0; i < 3; ++i)
    {
        a.Add(u3);
        b.Add(0);
    }

you are adding u3 3 times. that means a[0], a[1], a[2] all point to the same single list. If you modify a[0] that same change is done to a[1] and a[2].

You need to make a new list for each element

    for (int i = 0; i < 3; ++i)
    {
        List<double> u3 = new List<double>();
        for (int j=0;j<3;++j)
            u3.add(0);
        a.Add(u3);
        b.Add(0);
    }

However it looks like you are just working with fixed sized lists, your initialization code could be simplified if you just used arrays instead of lists

    List<double> coefs = new List<double>();
    double[,] a = new double[3,3];
    double[] b = new double[3];
    List<double> x2 = new List<double>();
    List<double> x3 = new List<double>();
    List<double> x4 = new List<double>();
    List<double> xy = new List<double>();
    List<double> x2y = new List<double>();

        for (int i = 0; i < n; ++i)
        {
            x2.Add(Math.Pow(x[i], 2));
            x3.Add(Math.Pow(x[i], 3));
            x4.Add(Math.Pow(x[i], 4));
            xy.Add(x[i] * y[i]);
            x2y.Add(Math.Pow(x[i], 2) * y[i]);
        }
    a[0,0] = n;
    a[0,1] = sum(x, n);
    a[0,2] = sum(x2, n);
    a[1,0] = a[0,1];
    a[1,1] = a[0,2];
    a[1,2] = sum(x3, n);
    a[2,0] = a[0,2];
    a[2,1] = a[1,2];
    a[2,2] = sum(x4, n);
    b[0] = sum(y, n);
    b[1] = sum(xy, n);
    b[2] = sum(x2y, n);

Upvotes: 3

SLaks
SLaks

Reputation: 887365

        a.Add(u3);

You just added the same inner list as all three items in a.

You need to add a separate List<double> instance in each iteration.

Upvotes: 2

Related Questions