黑马程序员技术交流社区
标题:
我想按成绩排序输出,可是运行结果不对,哪位帮忙看看
[打印本页]
作者:
张小庆
时间:
2012-4-3 12:51
标题:
我想按成绩排序输出,可是运行结果不对,哪位帮忙看看
import java.util.*;
class Cjd
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("李雷",18,95));
ts.add(new Student("韩梅梅",19,97));
ts.add(new Student("露西",17,96));
ts.add(new Student("莉莉",17,94));
ts.add(new Student("汤姆",18,94));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getResult()+"\t");
}
}
}
class Student implements Comparable
{
private String name;
private int age;
private int result;
Student(String name,int age,int result)
{
this.name=name;
this.age=age;
this.result=result;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setResult(int result)
{
this.result=result;
}
public int getResult()
{
return result;
}
public int compareTo(Object obj)
{
if (!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
if(this.result>s.result || this.result<s.result)
return 1;
if (this.result==s.result)
{
return this.name.compareTo(s.name);
}
return -1;
}
}
输出结果总是原样输出,不知道是哪儿出了问题?还是我这代码哪儿有问题啊?
作者:
τ、高童鞋ゝ
时间:
2012-4-3 13:15
先记住一个成绩最高值然后在排序
public static Person findMax(List<Person> list){
Person max = list.get(0);
for (Person p : list)
if(p.result() > max.result())
max = p;
return max;
}
作者:
陈超
时间:
2012-4-3 13:30
public int compareTo(Object obj)
{
if (!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
if(this.result>s.result || this.result<s.result)//
此处判断条件有问题
return 1;
if (this.result==s.result)
{
return this.name.compareTo(s.name);
}
return -1;
}
}
按照你这种条件判断排序,就只对成绩相同的对象进行排序。其他的就没有什么变化
作者:
陈苓
时间:
2012-4-3 14:22
修改如下:
import java.util.*;
class Cjd
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new);
ts.add(new Student("李雷",18,95));
ts.add(new Student("韩梅梅",19,97));
ts.add(new Student("露西",17,96));
ts.add(new Student("莉莉",17,94));
ts.add(new Student("汤姆",18,94));
Iterator it = ts.iterator( new compartorByResult);
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getResult()+"\t");
}
}
}
class Student {//
implements Comparable 这里实现这个借口比必要了
private String name;
private int age;
private int result;
Student(String name,int age,int result)
{
this.name=name;
this.age=age;
this.result=result;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setResult(int result)
{
this.result=result;
}
class compartorByLevel implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Result)o1;
Student s2 = (Result)o2;
int temp = s2.getResult()-s1.getResult();
return temp==0?s1.getName().compareTo(s2.getName()):temp;
}
}
作者:
邓飞飞
时间:
2012-4-3 14:24
本帖最后由 邓飞飞 于 2012-4-3 14:26 编辑
import java.util.*;
class Cjd
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("李雷",18,95));
ts.add(new Student("韩梅梅",19,97));
ts.add(new Student("露西",17,96));
ts.add(new Student("莉莉",17,94));
ts.add(new Student("汤姆",18,94));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getResult()+"\t");
}
}
}
class Student implements Comparable
{
private String name;
private int age;
private int result;
Student(String name,int age,int result)
{
this.name=name;
this.age=age;
this.result=result;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setResult(int result)
{
this.result=result;
}
public int getResult()
{
return result;
}
public int compareTo(Object obj)
{
if (!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
if(this.result>s.result )
//你的代码在这儿错了,修改后是按照从小到大排列,你如果想从大到小排,只需改成
if(this.result<s.result)
return 1;
if (this.result==s.result)
{
return this.name.compareTo(s.name);
}
return -1;
}
}
我已经调试过了,这是在你的源代码上改的!
作者:
张小庆
时间:
2012-4-3 17:02
谢谢大家了,明白了
作者:
薛飞飞
时间:
2012-4-3 18:54
你判断条件出错了,比较两个人的成绩应该是:this.getResult()和s.getResult()相比
详情见下面代码:
public int compareTo(Object obj)
{
if (!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
int num = this.getResult()-s.getResult();
if(num==0){
return this.getName().compareTo(s.getName());
}
else {
return num;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2