import.java.util,java的一段代码

本文提供了一个使用Java实现的学生信息类(Student),该类实现了Comparator和Comparable接口以支持不同条件下的排序。示例展示了如何通过TreeSet集合来对学生的姓名、年龄及分数进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.*;

public class Student implements Comparator,Comparable{

private String name;

pr

import java.util.*;

public class Student implements Comparator,Comparable{

private String name;

private int age;

private double score;

Student(String name,int age,double score)

{

this.name=name;

this.age=age;

this.score=score;

}

public int compareTo(Student s){

int num = new Double(this.score).compareTo(new Double(s.score));

if(num==0)

return this.name.compareTo(s.name);

return num;

}

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

public double getScore()

{

return score;

}

public void setName(String name)

{

this.name =name;

}

public void setAge(int age)

{

this.age=age;

}

public void setScore(double score)

{

this.score=score;

}

public int compare(Student s1, Student s2) {

// TODO Auto-generated method stub

int num = s1.getScore().compareTo(s2.getScore());

if(num==0)

return new Double(s1.getScore()).compareTo(new Double(s2.getScore()));

return num;

}

public static void main(String[] args){

TreeSet ts = new TreeSet();

ts.add(new Student("zhangsan",20,85.5));

ts.add(new Student("lisi",21,82.5));

ts.add(new Student("wnagwu",25,90.0));

ts.add(new Student("zhaoliu",22,88.5));

ts.add(new Student("biqi",22,95.0));

Iterator it = ts.iterator();

while (it.hasNext())

{

Student stu =(Student)it.next();

System.out.println(stu.getName()+"....."+stu.getAge()+"..."+stu.getScore());

}

}

}

这段代码报错怎么解决?

展开

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值