黑马程序员技术交流社区

标题: JAVA TreeSet添加对象有问题 [打印本页]

作者: hzhzhen    时间: 2015-5-18 12:33
标题: JAVA TreeSet添加对象有问题
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.Random;
  5. import java.util.TreeSet;

  6. public class px {
  7. Random rd;
  8. TreeSet ts;
  9. TreeSet ts1;
  10. TreeSet ts2;
  11. TreeSet ts3;
  12. TreeSet ts4;
  13. px(){
  14.   rd=new Random();
  15.   //ts=new TreeSet();
  16.   
  17.   ts=new TreeSet(new Comparator(){

  18.    public int compare(Object o1, Object o2) {
  19.     player p1=(player)o1;
  20.     player p2=(player)o2;
  21.     return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
  22.    }
  23.    
  24.   });
  25.   
  26.   ts1=new TreeSet(new Comparator(){

  27.    public int compare(Object o1, Object o2) {
  28.     player p1=(player)o1;
  29.     player p2=(player)o2;
  30.     return p1.money<p2.money?1:p1.money<p2.money?-1:0;
  31.    }
  32.    
  33.   });
  34.   
  35.   ts2=new TreeSet(new Comparator(){

  36.    public int compare(Object o1, Object o2) {
  37.     player p1=(player)o1;
  38.     player p2=(player)o2;
  39.     return p1.wood<p2.wood?1:p1.wood<p2.wood?-1:0;
  40.    }
  41.    
  42.   });
  43.   
  44.   ts3=new TreeSet(new Comparator(){

  45.    public int compare(Object o1, Object o2) {
  46.     player p1=(player)o1;
  47.     player p2=(player)o2;
  48.     return p1.sin<p2.sin?1:p1.sin<p2.sin?-1:0;
  49.    }
  50.    
  51.   });
  52.   
  53.   ts4=new TreeSet(new Comparator(){

  54.    public int compare(Object o1, Object o2) {
  55.     player p1=(player)o1;
  56.     player p2=(player)o2;
  57.     return p1.death<p2.death?1:p1.death<p2.death?-1:0;
  58.    }
  59.    
  60.   });
  61. }

  62. public void generate(){
  63.   for(int i=0;i<5;i++){
  64.    player p=new player(rd.nextInt(100),rd.nextInt(100),rd.nextInt(100),rd.nextInt(100));
  65.    add(p);
  66.   }
  67. }
  68. public void add(player p){
  69.   ts.add(p);
  70.   ts1.add(p);
  71.   ts2.add(p);
  72.   ts3.add(p);
  73.   ts4.add(p);
  74. }
  75. public void show(TreeSet ts){
  76.   System.out.println(ts.size());
  77.   Iterator it=ts.iterator();
  78.   while(it.hasNext()){
  79.    ((player)it.next()).show();
  80.   }
  81.   System.out.println();
  82. }
  83. public static void main(String[] args) {
  84.   px p=new px();
  85.   p.generate();
  86.   p.show(p.ts);
  87. }
  88. }
  89. class player/* implements Comparator*/{
  90. int money;
  91. int wood;
  92. int sin;
  93. int death;
  94. int whole;
  95. player(int money,int wood,int sin,int death){
  96.   this.money=money;
  97.   this.wood=wood;
  98.   this.sin=sin;
  99.   this.death=death;
  100.   whole=money+wood+sin+death;
  101. }
  102. public void show(){
  103.   System.out.print("金币:"+money+'\t');
  104.   System.out.print("木材:"+wood+'\t');
  105.   System.out.print("罪恶值:"+sin+'\t');
  106.   System.out.print("死亡数:"+death+'\t');
  107.   System.out.println("总分:"+whole);
  108. }
  109. /*public int compare(Object o1, Object o2) {
  110.   player p1=(player)o1;
  111.   player p2=(player)o2;
  112.   return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
  113. }*/
  114. }
复制代码

为什么我生成的对象没有全部被添加进去?而且ts,ts1,ts2,ts3,ts4每个Set里保存对象的数量都不一定。
求解?
作者: JARVIS_YJ    时间: 2015-5-18 20:38
好复杂的代码。。。。。。。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2