求一个:去除集合里面重复的数据,请用VS2005来,不用拉姆达表达式

YellowManDog 2010-12-03 10:22:58
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; }
}
}
...全文
302 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tian1989 2010-12-03
  • 打赏
  • 举报
回复

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;
}
YellowManDog 2010-12-03
  • 打赏
  • 举报
回复
那个this到了我这里就出错啊,删了,就没用了
tian1989 2010-12-03
  • 打赏
  • 举报
回复
static IEnumerable<Student> eliminate(List<Student> ss)
{
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;
}
q107770540 2010-12-03
  • 打赏
  • 举报
回复
使用自定义扩展方法
给出完整代码:


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);
}
}



}
}
}
whrspsoft3723 2010-12-03
  • 打赏
  • 举报
回复
(1)这个叫拉姆表达式吗?
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>中,加完后,里边的数据就是不重复数据了。
YellowManDog 2010-12-03
  • 打赏
  • 举报
回复
能给个代码吗?实在是想不出来了
ruanwei1987 2010-12-03
  • 打赏
  • 举报
回复
冒泡排序!!!
ruanwei1987 2010-12-03
  • 打赏
  • 举报
回复
二楼说的很清楚了
YellowManDog 2010-12-03
  • 打赏
  • 举报
回复
二楼能说清楚点吗?
Teng_s2000 2010-12-03
  • 打赏
  • 举报
回复
循环,加一个bool进行判断,已经存在就跳过
rufus_lee 2010-12-03
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100604/23/d0700f64-8a1b-4684-9127-d915ea38f50b.html
龍过鸡年 2010-12-03
  • 打赏
  • 举报
回复
8楼的方法很好,建议楼主用那个

我出个偷懒的方法 :p



public class Student
{
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
wuyq11 2010-12-03
  • 打赏
  • 举报
回复
class Test
{
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;
}
}
比较对象属性值

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧