自定义的一个类
public class PixelColor {
Point point;
Color color; public PixelColor(Point Point,Color Color)
{
this.point = Point;
this.color = Color;
}
}
需要定义一个此对象的集合类:public class PixelColorCollection : List<PixelColor> {} new多个PixelColor类的对象 添加到 两个List<T>里面。 然后比较这两个List<T> 是否相等。
判断相等的条件:1,两个List<T> 内的PixelColor对象个数相等 2,List<T> 内每个PixelColor对象的point 和color的数值相等3. List<T> 内PixelColor对象的顺序可以不一样。4.我认为代码应该具有如下样式:
public class PixelColorCollection : List<PixelColor>
{
public override bool Equals(object obj)
{ }
}也不知道对不对,请各位朋友不吝赐教
public class PixelColor {
Point point;
Color color; public PixelColor(Point Point,Color Color)
{
this.point = Point;
this.color = Color;
}
}
需要定义一个此对象的集合类:public class PixelColorCollection : List<PixelColor> {} new多个PixelColor类的对象 添加到 两个List<T>里面。 然后比较这两个List<T> 是否相等。
判断相等的条件:1,两个List<T> 内的PixelColor对象个数相等 2,List<T> 内每个PixelColor对象的point 和color的数值相等3. List<T> 内PixelColor对象的顺序可以不一样。4.我认为代码应该具有如下样式:
public class PixelColorCollection : List<PixelColor>
{
public override bool Equals(object obj)
{ }
}也不知道对不对,请各位朋友不吝赐教
{
PixelColorCollection anotherObj = (PixelColorCollection )obj;
return this.point.Equals(anotherObj.point);
}
public class PixelColorCollection: List<PixelColor> {
public static Boolean operator ==(PixelColorCollection dFirst, PixelColorCollection dSecond) {
if (dFirst.Count != dSecond.Count) return false;
foreach (String aKey in dFirst.Keys) {
if (dSecond.ContainsKey(aKey) == false) return false;
if (dFirst[aKey] != dSecond[aKey]) return false;
}
return true;
} public static Boolean operator !=(PixelColorCollection dFirst, PixelColorCollection dSecond) {
return !(dFirst == dSecond);
}
}
{
public Point Point { get; set; }
public Color Color { get; set; } public PixelColor(Point point, Color color)
{
this.Point = point;
this.Color = color;
}
}public class PixelColorCollection : List<PixelColor>
{
public static bool Compare(PixelColorCollection c1, PixelColorCollection c2)
{
if (c1 == c2) return true; if (c1 == null || c2 == null || c1.Count != c2.Count) return false; var s1 = string.Join(",", c1
.Select(item => item.Point.ToString() + item.Color.ToString())
.OrderBy(element => element).ToArray()); var s2 = string.Join(",", c2
.Select(item => item.Point.ToString() + item.Color.ToString())
.OrderBy(element => element).ToArray()); return s1 == s2;
}
}
PixelColorCollection c1 = new PixelColorCollection();
PixelColorCollection c2 = new PixelColorCollection();
PixelColorCollection c3 = new PixelColorCollection();
PixelColorCollection c4 = new PixelColorCollection();
PixelColorCollection c5 = new PixelColorCollection();c1.AddRange(new PixelColor[] {
new PixelColor(new Point(0,0), Colors.Red),
new PixelColor(new Point(0,0), Colors.Yellow),
new PixelColor(new Point(0,0), Colors.Blue)
});c2.AddRange(new PixelColor[] {
new PixelColor(new Point(0,0), Colors.Yellow),
new PixelColor(new Point(0,0), Colors.Blue),
new PixelColor(new Point(0,0), Colors.Red)
});c3.AddRange(new PixelColor[] {
new PixelColor(new Point(0,0), Colors.Yellow),
new PixelColor(new Point(0,0), Colors.Blue)
});c4.AddRange(new PixelColor[] {
new PixelColor(new Point(0,0), Colors.Yellow),
new PixelColor(new Point(0,0), Colors.Blue),
new PixelColor(new Point(0,0), Colors.Red),
new PixelColor(new Point(0,0), Colors.Green)
});c5.AddRange(new PixelColor[] {
new PixelColor(new Point(0,0), Colors.Yellow),
new PixelColor(new Point(0,0), Colors.Blue),
new PixelColor(new Point(0,1), Colors.Red)
});Console.WriteLine(c1 == c2);
Console.WriteLine(c1 == c3);
Console.WriteLine(c1 == c4);
Console.WriteLine(c1 == c5);
Console.WriteLine("-----------------");
Console.WriteLine(PixelColorCollection.Compare(c1, c2));
Console.WriteLine(PixelColorCollection.Compare(c1, c3));
Console.WriteLine(PixelColorCollection.Compare(c1, c4));
Console.WriteLine(PixelColorCollection.Compare(c1, c5));至于利用 hash 及重载操作符的方法可以参考http://blog.csdn.net/alex197963/archive/2007/04/22/1574893.aspx