黑马程序员技术交流社区
标题:
JAVA TreeSet添加对象有问题
[打印本页]
作者:
hzhzhen
时间:
2015-5-23 10:54
标题:
JAVA TreeSet添加对象有问题
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class px {
Random rd;
TreeSet ts;
TreeSet ts1;
TreeSet ts2;
TreeSet ts3;
TreeSet ts4;
px(){
rd=new Random();
//ts=new TreeSet();
ts=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
}
});
ts1=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.money<p2.money?1:p1.money<p2.money?-1:0;
}
});
ts2=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.wood<p2.wood?1:p1.wood<p2.wood?-1:0;
}
});
ts3=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.sin<p2.sin?1:p1.sin<p2.sin?-1:0;
}
});
ts4=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.death<p2.death?1:p1.death<p2.death?-1:0;
}
});
}
public void generate(){
for(int i=0;i<5;i++){
player p=new player(rd.nextInt(100),rd.nextInt(100),rd.nextInt(100),rd.nextInt(100));
add(p);
}
}
public void add(player p){
ts.add(p);
ts1.add(p);
ts2.add(p);
ts3.add(p);
ts4.add(p);
}
public void show(TreeSet ts){
System.out.println(ts.size());
Iterator it=ts.iterator();
while(it.hasNext()){
((player)it.next()).show();
}
System.out.println();
}
public static void main(String[] args) {
px p=new px();
p.generate();
p.show(p.ts);
}
}
class player/* implements Comparator*/{
int money;
int wood;
int sin;
int death;
int whole;
player(int money,int wood,int sin,int death){
this.money=money;
this.wood=wood;
this.sin=sin;
this.death=death;
whole=money+wood+sin+death;
}
public void show(){
System.out.print("金币:"+money+'\t');
System.out.print("木材:"+wood+'\t');
System.out.print("罪恶值:"+sin+'\t');
System.out.print("死亡数:"+death+'\t');
System.out.println("总分:"+whole);
}
/*public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
}*/
}
复制代码
作者:
pvbj0314
时间:
2015-5-23 22:06
比较规则有问题把“ p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;”中的第二个“<”改为>。类似的都修改。下面是我改过的,5个Player都有,你试试:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class PX {
Random rd;
TreeSet ts;
TreeSet ts1;
TreeSet ts2;
TreeSet ts3;
TreeSet ts4;
PX(){
rd=new Random();
//ts=new TreeSet();
ts=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.whole<p2.whole?1:p1.whole>p2.whole?-1:0;
}
});
ts1=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.money<p2.money?1:p1.money>p2.money?-1:0;
}
});
ts2=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.wood<p2.wood?1:p1.wood>p2.wood?-1:0;
}
});
ts3=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.sin<p2.sin?1:p1.sin>p2.sin?-1:0;
}
});
ts4=new TreeSet(new Comparator(){
public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.death<p2.death?1:p1.death>p2.death?-1:0;
}
});
}
public void generate(){
for(int i=0;i<5;i++){
player p=new player(rd.nextInt(100),rd.nextInt(100),rd.nextInt(100),rd.nextInt(100));
add(p);
}
}
public void add(player p){
ts.add(p);
ts1.add(p);
ts2.add(p);
ts3.add(p);
ts4.add(p);
}
public void show(TreeSet ts){
System.out.println(ts.size());
Iterator it=ts.iterator();
while(it.hasNext()){
((player)it.next()).show();
}
System.out.println();
}
public static void main(String[] args) {
PX p=new PX();
p.generate();
p.show(p.ts);
p.show(p.ts1);
p.show(p.ts2);
p.show(p.ts3);
p.show(p.ts4);
}
}
class player/* implements Comparator*/{
int money;
int wood;
int sin;
int death;
int whole;
player(int money,int wood,int sin,int death){
this.money=money;
this.wood=wood;
this.sin=sin;
this.death=death;
whole=money+wood+sin+death;
}
public void show(){
System.out.print("金币:"+money+'\t');
System.out.print("木材:"+wood+'\t');
System.out.print("罪恶值:"+sin+'\t');
System.out.print("死亡数:"+death+'\t');
System.out.println("总分:"+whole);
}
/*public int compare(Object o1, Object o2) {
player p1=(player)o1;
player p2=(player)o2;
return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
}*/
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2