c#嵌套for循环检查比较列表中的每个元素返回奇数结果

时间:2016-11-17 04:53:04

标签: c# list loops nested compare

所以我有这个代码......这让我发疯了。结果不一致,它取决于列表中对象的顺序。

基本上复制一个对象列表,使用compareMembers()检查列表中的每个对象(100%没有问题)然后如果它们比允许(num)有更多共同点那么它们就是"类似"我们决定删除哪一个。接下来,从临时列表中删除较低分数,并在所有迭代后返回临时列表。

示例,如果我在删除类似之前对列表进行排序:

group.Sort((a, b) => -1 * a.GroupScore().CompareTo(b.GroupScore()));

返回的列表中的元素总是少于我比较类似的元素,并且列表是完全随机的。我知道问题出在这一部分......希望它的内容很简单,而且我已经盯着它看了太长时间。

以下是产生冲突结果的代码。

    public List<MyGroup> RemoveSimilar(List<MyGroup> group, int num)
    {
        List<MyGroup> temp = group.ToList();

        for(int i = 0; i < group.Count - 1;i++)
        {
            for (int j = i + 1; j < group.Count;j++)
            {
                   if (group[i].compareMembers(group[j]) > num)
                    {
                        if (group[i].score < group[j].score)
                        {
                            temp.Remove(group[i]);
                            break;//removed this one might as well stop checking it
                        }
                        else
                        {
                            temp.Remove(group[j]);
                        }
                    }
                }
            }
        return temp;
    }

这是MyGroup类:

    public class MyGroup
{
    public Member firstMember;
    public Member secondMember;
    public Member thirdMember;
    public double score;

    public MyGroup()
    {
    }

    public int compareMembers(MyGroup x)
    {

        int i = 0;

        if (this.firstMember == x.firstMember)
        { i++; }
        if (this.secondMember == x.secondMember)
        { i++; }
        if (this.thirdMember == x.thirdMember)
        { i++; }
        if (this.firstMember == x.secondMember)
        { i++; }
        if (this.firstMember == x.thirdMember)
        { i++; }
        if (this.secondMember == x.thirdMember)
        { i++; }

        return i;
    }


    public MyGroup(Member one, Member two, Member three)
    {
        firstMember = one;
        secondMember = two;
        thirdMember = three;


        groupScore = firstMember.mScore + secondMember.mScore + thirdMember.mScore;
    }

    public double GroupScore()
    {

        return score;
    }
}

以下是用于组成小组的成员的快速课程:

    public class Member
{
    public string idNum;
    public string firstName;
    public string lastName;
    public double mScore;


    public Member()
    {
    }

    public Member(string id, string fnm, string lnm, double pt)
    {
        idNum = id;
        firstName = fnm;
        lastName = lnm;
        mScore = pt;
    }

    public override string ToString()
    { return firstName + " " + lastName; }


    public string Desc()
    {
        return idNum + " " + firstName + " " + mScore.ToString();
    }
 }

好的,所以我重新做了一点,似乎得到了一致的结果,无论列表的顺序如何......仍然很想知道为什么第一次尝试一直搞砸了。这是重新制作的版本:

        public List<MyGroup> RemoveSimilar(List<MyGroup> group, int num)
    {
        List<MyGroup> temp = group.ToList();//take group argument and copy it into a new list

        for (int i = 0; i < group.Count - 1; i++)//first loop through each element of group list
        {
            if (temp.Contains(group[i])) //checks to make sure group[i] hasn't already been removed from temp list
            {
                for (int j = i + 1; j < group.Count; j++)//second loop through each element to compare to first
                {
                    if(group[i] != group[j]) //make sure we aren't comparing the same two objects
                    {
                        if (group[i].compareMembers(group[j]) > num)//check to see how "similar" groups are
                        {
                            if (group[i].score < group[j].score)//if the groups are similar, see which has a higher score
                            {
                                temp.Remove(group[i]);//outer element is not the best "unique group" so we remove it
                                break; //removed this one might as well stop checking it
                            }
                            else
                            {
                                temp.Remove(group[j]);//inner element was similar to outter, but not the best so we remove it
                            }
                        }
                    }
                }
            }
        }
        return temp; //return the list after all similar elements have been removed
    }

1 个答案:

答案 0 :(得分:0)

当你有一个如下定义的函数时:

DES

使用参数更改调用对象时,请务必考虑这种情况。您通常希望以下调用获得相同的结果:

class MyGroup 
{
   public int compareMembers(MyGroup x);
}

在您的情况下,您需要将函数compareMembers更改为:

x.compareMembers(y);
y.compareMembers(x);

作为旁注,函数名称可以是countEqualMembers,以便更清楚地说明函数的用途。