为没事treeset,存代码,我只存进去一个呢,我命名写了4哥对象的呀??????- package jihe;
- import java.util.*;
- public 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));
- Iterator it = ts.iterator();
- /*while (it.hasNext()) {
- student stu = (student) it.next();
- System.out.println(stu.getName() + "..." + stu.getAge());
- System.out.println("treeset ts里放的元素的个数是:"+ts.size()+"个");
- }*/
- for(int i=0;i<ts.size();i++){
- System.out.println("ts集合的元素是"+ts.toString()+"ts集合的size()是"+ts.size());
- }
- }
- }
- 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 + "..compare TO.." + s.name);
- if (this.age > s.age)
- return 1;
- if (this.age == s.age)
- return this.name.compareTo(s.name);
- return -1;
- }
- public int getAge() {
- // TODO Auto-generated method stub
- return age;
- }
- public String getName() {
- // TODO Auto-generated method stub
- return name;
- }
- // @Override
- /*
- * public int compareTo(Object o) { // TODO Auto-generated method stub
- * return 0; }
- */
- /*
- * @Override public int compareTo(Object o) { // TODO Auto-generated method
- * stub return 0; }
- */
- @Override
- public int compareTo(Object o) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
复制代码 ??????????????????????????????奇怪 |