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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我为你着迷 金牌黑马   /  2014-7-27 10:16  /  1043 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class Student implements Comparable
  3. {
  4.         private String name;
  5.   private int age;
  6.   
  7.   Student(String name,int age)
  8.   {
  9.           this.name=name;
  10.           this.age=age;       
  11.   }
  12.   
  13.   public int compareTo(Object obj)
  14.   {
  15.           if(!(obj instanceof Student))
  16.                           throw new RuntimeException("不是学生对象");
  17.           Student s=(Student)obj;
  18.          
  19.           if(this.age>s.age)
  20.                           return 1;
  21.           if(this.age==s.age)
  22.           {
  23.                   return this.name.compareTo(s.name);       
  24.           }
  25.           return -1;
  26.   }
  27.   
  28.   public String getName()
  29.   {
  30.           return name;       
  31.   }
  32.   
  33.   public int getAge()
  34.   {
  35.           return age;       
  36.   }
  37. }


  38. class TreeSetDemo2
  39. {
  40.         public static void main(String[] args)
  41.         {
  42.                 TreeSet ts=new TreeSet(new MyCompare());
  43.                
  44.                 ts.add(new Student("lisi02",22));
  45.                 ts.add(new Student("lisi007",20));
  46.                 ts.add(new Student("lisi09",19));
  47.                 ts.add(new Student("lisi06",18));
  48.                 ts.add(new Student("lisi007",29));
  49.                
  50.                 Iterator it=ts.iterator();
  51.                 while(it.hasNext())
  52.                 {
  53.                         Student stu=(Student)it.next();
  54.                         System.out.println(stu.getName()+"...."+stu.getAge());       
  55.                 }
  56.   }
  57. }


  58. class MyCompare implements Comparator
  59. {
  60.         public int compare(Object o1,Object o2)
  61.         {
  62.                 Student s1=(Student)o1;
  63.                 Student s2=(Student)o2;
  64.                
  65.                 int num=s1.getName().compareTo(s2.getName());
  66.                 if(num==0)
  67.                 {
  68.                   return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));       
  69.                 }
  70.                
  71.                 return num;
  72.         }       
  73. }
复制代码
大家好,以上代码怎么加入泛型呢? 麻烦仁兄帮忙演示下,好吗
虽然学习了泛型技术,但是还是不知道怎么用,例如视频里泛型之前的演示代码
HashSet代码  ArrayList代码 TreeSet代码都还是不知道怎么加泛型。表示很是捉急啊
怎么办啊 帮帮我 好吗
例如
TreeSet ts=new TreeSet(new MyCompare());
ts.add(new Student("lisi02",22));
这块怎么加泛型呢? 添加的元素里边既有字符串又有基本数据类型Integer 唉 很郁闷
另外帮忙解决问题的 讲的详细的  给你10个黑马币 谢谢


评分

参与人数 1技术分 +1 收起 理由
格子、 + 1 淡定

查看全部评分

8 个回复

正序浏览
郑飞 高级黑马 2014-7-27 17:58:32
8#
我为你着迷 发表于 2014-7-27 12:45
你这个我表示没看大懂呢   MyCompare com = new MyCompare();  这个是集合容器吗   还有@Override  这个 ...

楼主明显视频看少了 这个看名字就该是自己写的比较器实现类啊 老毕视频有 不过我也就看懂8-9成 估计得多用才能测底搞懂了
回复 使用道具 举报
我为你着迷 发表于 2014-7-27 12:45
你这个我表示没看大懂呢   MyCompare com = new MyCompare();  这个是集合容器吗   还有@Override  这个 ...

这个是比较器的实现,比较器不是个接口么【MyCompare implements Comparator<Student> 】,MyCompare就是把它实现了。@Override  【  public int compare(Student o1, Student o2) 】就是代表我的实现类MyCompare重写了比较器接口的方法compare

点评

没事没事……我现在也是为了多挣技术分,赶41期。呵呵  发表于 2014-7-27 14:40

评分

参与人数 1黑马币 +5 收起 理由
我为你着迷 + 5 嗯 很开心 你为我解答问题 在给你5个黑马.

查看全部评分

回复 使用道具 举报
洛漠O_o 发表于 2014-7-27 12:16
==============================================================

import java.util.*;

你这个我表示没看大懂呢   MyCompare com = new MyCompare();  这个是集合容器吗   还有@Override  这个是什么意思啊  
回复 使用道具 举报
我为你着迷 发表于 2014-7-27 11:46
我试了下你这个   可以了   那在这个代码中能不能帮我演示下comparable和比较器怎么加泛型呀 谢谢你啊  {: ...

网速不太好,……
回复 使用道具 举报
我为你着迷 发表于 2014-7-27 11:46
我试了下你这个   可以了   那在这个代码中能不能帮我演示下comparable和比较器怎么加泛型呀 谢谢你啊  {: ...

==============================================================

import java.util.*;

public class Test {
        public static void main(String[] args) {
                MyCompare com = new MyCompare();
                if (com.compare(new Student(0, "小明", 21), new Student(1,"小李", 22)) == -1) {
                        System.out.println("小明没有小李大");
                }else if (com.compare(new Student(0, "小明", 21), new Student(1,"小李", 22)) == 1) {
                        System.out.println("小明比小李大");
                }else if (com.compare(new Student(0, "小明", 21), new Student(1,"小李", 22)) == 0) {
                        System.out.println("小明和小李一样大");
                }
                System.out.println();

        }
}

class MyCompare implements Comparator<Student> {
        //加了泛型之后,compare参数就是Student了
        @Override
        public int compare(Student o1, Student o2) {
                if (o1.getAge() > o2.getAge()) {
                        return 1;
                } else if (o1.getAge() == o2.getAge()) {
                        return 0;
                } else {
                        return -1;
                }
        }

}

class Student {
        private int id;
        private String name;
        private int age;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

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

        public int getAge() {
                return age;
        }

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

        public Student() {
                super();
        }

        public Student(int id, String name, int age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
        }

}
===========================================================
回复 使用道具 举报
本帖最后由 我为你着迷 于 2014-7-27 11:52 编辑
洛漠O_o 发表于 2014-7-27 11:34
看下,能不能解决问题
================================================================
public class T ...

我试了下你这个   可以了   那在这个代码中能不能帮我演示下comparable和比较器怎么加泛型呀 谢谢你啊  {:3_64:}
      
回复 使用道具 举报 1 0
看下,能不能解决问题
================================================================
public class Test {
        public static void main(String[] args) {
                TreeSet ts = new TreeSet(new MyCompare());

                ts.add(new Student("lisi02", 22));
                ts.add(new Student("lisi007", 20));
                ts.add(new Student("lisi09", 19));
                ts.add(new Student("lisi06", 18));
                ts.add(new Student("lisi007", 29));

                Iterator it = ts.iterator();
                while (it.hasNext()) {
                        Student stu = (Student) it.next();
                        System.out.println(stu.getName() + "...." + stu.getAge());
                }
                System.out.println("====================使用了泛型====================");
                //声明父类创建子类,用下泛型
                Set<Student> set = new TreeSet<Student>();
                set.add(new Student("lisi02", 22));
                set.add(new Student("lisi007", 20));
                set.add(new Student("lisi09", 19));
                set.add(new Student("lisi06", 18));
                set.add(new Student("lisi007", 29));
                //因为上面用了泛型所以
                Iterator<Student> is = set.iterator();
                while (is.hasNext()) {
                        Student student = is.next();//这里就不用类型强制转换了
                        System.out.println(student.getName()+"--->"+student.getAge());
                }
                System.out.println("====================我一般是这样用的====================");
                Set<Student> myset = new TreeSet<Student>();//当然使用了泛型之后,你添加其他对象就会报错的如myset.add(new Object());
                myset.add(new Student("lisi02", 22));
                myset.add(new Student("lisi007", 20));
                myset.add(new Student("lisi09", 19));
                myset.add(new Student("lisi06", 18));
                myset.add(new Student("lisi007", 29));
                for (Student student : myset) {
                        System.out.println(student.getName()+"--->"+student.getAge());
                }
               
        }
}
/**
* @title 关于Student类写法的一些建议
* @author Administrator
*
*/
class Student implements Comparable {
        private String name;
        private int age;
        //1.一般类都是有默认构造方法的,就是不带参数的构造方法。类操作多了,你就知道好处了
        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;
   
                if (this.age > s.age)
                        return 1;
                if (this.age == s.age) {
                        return this.name.compareTo(s.name);
                }
                return -1;
        }
        //2.get set方法一般也都是成对出现的,编辑器自动生成方式:alt+shift+s -> generate Getters and Setters ->选择变量点击【ok】
        //构造方法也可以自动生成
        public String getName() {
                return name;
        }

        public int getAge() {
                return age;
        }
}

class MyCompare implements Comparator {
        public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;

                int num = s1.getName().compareTo(s2.getName());
                if (num == 0) {
                        return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
                }

                return num;
        }
}
=================================================================

评分

参与人数 2技术分 +1 黑马币 +10 收起 理由
格子、 + 1 赞一个!
我为你着迷 + 10 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马