class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
Student stu1 = new Student(17, 170);
students.Add(stu1);
Student stu2 = new Student(18, 180);
students.Add(stu2);
Student stu1 = new Student(17, 170);
students.Add(stu3);
Student stu1 = new Student(18, 170);
students.Add(stu3);
Student stu1 = new Student(19, 190);
students.Add(stu4);
Student stu1 = new Student(22, 186);
students.Add(stu4);
//请写一个方法,去除里面重复的数据,并且返回一个新的集合(去除重复后的集合<重复:年龄和身高都相同>)
Console.ReadLine();
}
}
class Student
{
public Student() { } public Student(int age, double stature)
{
this._age = age;
this._stature = stature;
} private int _age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _age; }
set { _age = value; }
} private double _stature;
/// <summary>
/// 身高
/// </summary>
public double Stature
{
get { return _stature; }
set { _stature = value; }
}
}
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
Student stu1 = new Student(17, 170);
students.Add(stu1);
Student stu2 = new Student(18, 180);
students.Add(stu2);
Student stu1 = new Student(17, 170);
students.Add(stu3);
Student stu1 = new Student(18, 170);
students.Add(stu3);
Student stu1 = new Student(19, 190);
students.Add(stu4);
Student stu1 = new Student(22, 186);
students.Add(stu4);
//请写一个方法,去除里面重复的数据,并且返回一个新的集合(去除重复后的集合<重复:年龄和身高都相同>)
Console.ReadLine();
}
}
class Student
{
public Student() { } public Student(int age, double stature)
{
this._age = age;
this._stature = stature;
} private int _age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _age; }
set { _age = value; }
} private double _stature;
/// <summary>
/// 身高
/// </summary>
public double Stature
{
get { return _stature; }
set { _stature = value; }
}
}
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 ,21,21}; IEnumerable<int> distinctAges = ages.Distinct(); this.Text = distinctAges.Count().ToString();(2)把你的元素加到Dictionary<int,int>中,加完后,里边的数据就是不重复数据了。
给出完整代码:
namespace WebApplication1
{ static class Extensions
{
internal static bool stuEquals(this Student x, Student y)
{
if (x.Age == y.Age && x.Stature == y.Stature)
{
return true;
}
else
{
return false;
}
}
internal static bool ContainsStu(this List<Student> list, Student stu)
{
bool isContains = false;
foreach (Student s in list)
{
if (s.stuEquals(stu))
{
isContains = true;
break;
}
}
return isContains;
}
}
public class Student
{
public Student(int age, double stature)
{
this._age = age;
this._stature = stature;
} private int _age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return _age; }
set { _age = value; }
} private double _stature;
/// <summary>
/// 身高
/// </summary>
public double Stature
{
get { return _stature; }
set { _stature = value; }
} }
public partial class CSSTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> students = new List<Student>();
Student stu1 = new Student(17, 170);
students.Add(stu1);
Student stu2 = new Student(18, 180);
students.Add(stu2);
Student stu3 = new Student(17, 170);
students.Add(stu3);
Student stu4 = new Student(18, 170);
students.Add(stu4);
Student stu5 = new Student(19, 190);
students.Add(stu5);
Student stu6 = new Student(22, 186);
students.Add(stu6); List<Student> Newstudents = new List<Student>();
foreach (Student stu in students)
{
if (!Newstudents.ContainsStu(stu))
{
Newstudents.Add(stu);
}
} }
}
}
{
for (int i = 0; i < ss.Count; i++)
{
for (int j = i + 1; j < ss.Count; j++)
{
if ((ss[i].Age == ss[j].Age) && (ss[i].Stature == ss[j].Stature))
{
ss.RemoveAt(j);
j--;
}
}
}
return ss;
}
static List<Student> GetDifferent(List<Student> ss)
{
List<Student> newStudent = new List<Student>();
for (int i = 0; i < ss.Count; i++)
{
if (!newStudent.Contains(ss[i]))
{
newStudent.Add(ss[i]);
}
}
return newStudent;
}
{
public int i;
} class TestComparer<T> : IEqualityComparer<T>
where T : Test
{ public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
public bool Equals(T t1, T t2)
{
return t1.i == t2.i;
}
}
比较对象属性值
{
public uint Age { get; set; }
public double Stature { get; set; } public Student(uint age, double stature)
{
this.Age = age;
this.Stature = stature;
}
}// Form_Load 测试代码
List<Student> students = new List<Student>();
students.Add(new Student(5, 5d));
students.Add(new Student(51, 3d));
students.Add(new Student(5, 5d));
students.Add(new Student(5, 5d));
students.Add(new Student(23, 5d));
students.Add(new Student(43, 25d));List<Student> copy = new List<Student>();//// 默认 Contains 方法无法甄别重复项目
//foreach(Student item in students)
//{
// if (copy.Contains(item)) continue;
// else copy.Add(item);
//}// 根据7楼的提议,弄了个偷懒的法子
Dictionary<uint, double> dic = new Dictionary<uint, double>();foreach(Student student in students)
{
if(!dic.ContainsKey(student.Age))
{
dic.Add(student.Age, student.Stature);
copy.Add(student);
continue;
}
if(!dic.ContainsValue(student.Stature))
{
dic.Add(student.Age, student.Stature);
copy.Add(student);
continue;
}
}Console.WriteLine("Count: {0}", copy.Count);// 输出 Count: 4