user1498052
user1498052

Reputation:

List.Clear() clears a list I'm not expecting it to

I would like to know why my templist.clear() method clears the list I added to my ManhattanDistance Dictionary.

Any help in this regard would be much appreciated, this is part of my data mining project which I have been working on. I have to impute the missing values using k nearest neighbor approach.

public void CalculateManhattanDistance(Dictionary<int, List<string>> MissingList, Dictionary<int, List<string>> OtherList)
{
    Dictionary<int,Array> MissingListNeighbours = new Dictionary<int,Array>();
    Dictionary<int, List<int>> ManhattanDistanceList = new Dictionary<int,List<int>>();
    List<int> tempList = new List<int>();

    int total=0;
    int k=0;

    try
    {
        for (int i = 0; i < MissingList.Count(); i++)
        {
            for (int j = 0; j < OtherList.Count(); j++)
            {
                for (k = 0; k < MissingList[0].ToArray().Length; k++)
                {
                    if (Convert.ToChar(MissingList[i][k].ToString()) == '?')
                        continue;
                    else
                        total += Math.Abs(Convert.ToInt32(MissingList[i][k].ToString()) - Convert.ToInt32(OtherList[j][k].ToString()));
                }
                tempList.Add(total);

                total = 0;
            }
            ManhattanDistanceList.Add(i, tempList);

            tempList.Clear();
        }
    }
    catch (Exception ex)
    {
          ex.Message.ToString();
    }
}

Upvotes: 0

Views: 174

Answers (2)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

Because you're adding the list object to the dictionary and then you're clearing the very same object that you added.

What you want, instead, is:

public void CalculateManhattanDistance(Dictionary<int, List<string>> MissingList, Dictionary<int, List<string>> OtherList)
    {
        Dictionary<int,Array> MissingListNeighbours = new Dictionary<int,Array>();
        Dictionary<int, List<int>> ManhattanDistanceList = new Dictionary<int,List<int>>();

        try
        {
            for (int i = 0; i < MissingList.Count(); i++)
            {
                List<int> tempList = new List<int>();
                for (int j = 0; j < OtherList.Count(); j++)
                {
                    int total=0;
                    for (int k = 0; k < MissingList[0].ToArray().Length; k++)
                    {
                        if (Convert.ToChar(MissingList[i][k].ToString()) == '?')
                            continue;
                        else
                            total += Math.Abs(Convert.ToInt32(MissingList[i][k].ToString()) - Convert.ToInt32(OtherList[j][k].ToString()));


                    }
                    tempList.Add(total);

                }
                ManhattanDistanceList.Add(i, tempList);

            }
        }
        catch (Exception ex)
        {
              ex.Message.ToString();
        }
    }

Make a habit of declaring variables at the scope in which they are needed and you won't run into this kind of problem often.

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151586

Because ManhattanDistanceList.Add(i, tempList); adds a reference to the same list tempList is pointing to, so when you later clear the list tempList is pointing to, ManhattanDistanceList[i] also gets cleared.

Change it to ManhattanDistanceList.Add(i, tempList.ToList()); to add a copy of the list.

Upvotes: 6

Related Questions