A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/*
毕老师,第15天——02的视频
需求:往TreeSet集合中存储自定义对象学生
想按照学生的年龄进行排序
*/
import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
  TreeSet ts = new TreeSet();
  
  ts.add(new Student("lisi02", 22));//建立对象,并传入参数
  ts.add(new Student("lisi007", 20));
  ts.add(new Student("lisi09", 19));
  ts.add(new Student("lisi01",19));
  //ts.add(new Student("lisi01",19));//年龄和姓名与上面的相同,视为同一个人,无法存入并输出
   
  Iterator it = ts.iterator();
  
  while(it.hasNext())
  {  
   Student stu = (Student)it.next();
   System.out.println(stu.getName()+"..."+stu.getAge());
  }
}
}
class Student implements Comparable //该接口强制让学生具备比较性
{
private String name;
private int age;
Student(String name, int age)
{
  this.name = name;
  this.age = age;
}

public int compareTo(Object obj)
{
  if(!(obj instanceof Student))
   throw new RuntimeException("不是学生对象");
  Student s = (Student)obj;
  
  System.out.println(this.name+"...compareto..."+s.name);//输出比较过程
  if(this.age>s.age)
   return 1;
  if(this.age==s.age)//当年龄相同时,按照姓名排序
  {
   return this.name.compareTo(s.name);
  }
  return -1;
}

public String getName()
{
  return name;
}
public int getAge()
{
  return age;
}
}
注意:以上代码是假设有相同年龄和姓名的人视为同一个人,年龄相同则按姓名排序,
我的疑惑:现实生活中有许多年龄和姓名一样的人,但不是同一个人试问:该怎么排序?我想从上面的程序进行修改,但是相同年龄和姓名的人视为同一个人,无法存入并输出,还有什么其他办法?

点评

如果问题已经解决了,请将分类改为已解决,谢谢  发表于 2013-3-15 08:24

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

4 个回复

正序浏览
首先说,毕老师的这段代码只是一种假设,在实际情况中确实不可能是这样的,那么在实际情况中是按照身份证的唯一性判断的,所以你不必纠结在这段代码上,因为这段TreeSet的示例仅仅是为你展现了怎样实现TreeSet排序的内部原理而已,如果你想符合实际情况,大可自己再编这段代码的时候把身份证作为Person的一个属性,那样就符合实际情况了啊。这不是技术问题,是常识问题,但愿你能理解。
回复 使用道具 举报
王芝 发表于 2013-3-14 16:41
楼上说的好啊,或者再加个学号也行,呵呵。

学号一般不会相同的,
回复 使用道具 举报
本帖最后由 白堇翎 于 2013-3-14 17:14 编辑

Set接口本身就是为了存储不重复的元素而存在的
假如你一定要用Set接口的话..
那可以给每个人加一个身份证号码 就可以实现这种功能了
  1. package Test;

  2. import java.util.Iterator;
  3. import java.util.TreeSet;

  4. public class IDDemo {

  5.         public static void main(String[] args) {
  6.                 TreeSet ts = new TreeSet();

  7.                 ts.add(new Student("lisi02", 22, 1001));// 建立对象,并传入参数
  8.                 ts.add(new Student("lisi007", 20, 1002));
  9.                 ts.add(new Student("lisi09", 19, 1003));
  10.                 ts.add(new Student("lisi01", 19, 1004));
  11.                 ts.add(new Student("lisi01", 19, 1005));// 年龄和姓名与上面的相同,视为同一个人,无法存入并输出

  12.                 Iterator it = ts.iterator();

  13.                 while (it.hasNext()) {
  14.                         Student stu = (Student) it.next();
  15.                         System.out.println(stu.getName() + "..." + stu.getAge() + "...."
  16.                                         + stu.getID());
  17.                 }
  18.         }
  19. }

  20. class Student implements Comparable // 该接口强制让学生具备比较性

  21. {
  22.         private String name;
  23.         private int age;
  24.         private int ID;

  25.         Student() {
  26.                 super();
  27.         }

  28.         Student(String name, int age, int ID) {
  29.                 super();
  30.                 this.name = name;
  31.                 this.age = age;
  32.                 this.ID = ID;
  33.         }

  34.         public int compareTo(Object obj) {
  35.                 if (!(obj instanceof Student))
  36.                         throw new RuntimeException("不是学生对象");
  37.                 Student s = (Student) obj;

  38.                 System.out.println(this.name + "...compareto..." + s.name);// 输出比较过程
  39.                 if ( this.age > s.age)
  40.                         return 1;
  41.                 if ((this.ID == s.ID) && (this.age == s.age))// 当年龄相同时,按照姓名排序
  42.                 {
  43.                         return this.name.compareTo(s.name);
  44.                 }
  45.                 return -1;
  46.         }

  47.         public void setName(String name) {
  48.                 this.name = name;
  49.         }

  50.         public void setAge(int age) {
  51.                 this.age = age;
  52.         }

  53.         public void setID(int ID) {
  54.                 this.ID = ID;
  55.         }

  56.         public String getName() {
  57.                 return name;
  58.         }

  59.         public int getAge() {
  60.                 return age;
  61.         }

  62.         public int getID() {
  63.                 return ID;
  64.         }
  65. }
复制代码
这是修改后的代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马