展開全部
編寫一個簡單的java程序?import?java.util.Arrays;
import?java.util.Collections;
import?java.util.List;
public?class?Baiduzhidao?{
public?static?void?main?(String?[]?args)
{
e69da5e6ba9062616964757a686964616f31333335333761Student[]?stuArr?=?{new?Student("001",?90),new?Student("002",?70),new?Student("003",?85),new?Student("004",?92),new?Student("005",?99)};
List?list?=?Arrays.asList(stuArr);
Collections.sort(list);
System.out.println("最低成績的學生:"+list.get(0));
System.out.println("==========================================");
for?(Student?student?:?list)?{
System.out.println(student);
}
}
}
//學生類
public?class?Student?implements?Comparable{
//?學號,名字,年齡,英語成績,計算機成績
private?String?stuNo;
private?int?score;
//構造函數、get/set省略
@Override
public?String?toString()?{
return?"Student?[學號:"?+?stuNo?+?",?成績:"?+?score?+?"]";
}
/*?(non-Javadoc)
*?@see?java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public?int?compareTo(Student?o)?{
return?this.score-o.score;
}
}
輸出:
最低成績的學生:Student [學號:002, 成績:70]
==========================================
Student [學號:002, 成績:70]
Student [學號:003, 成績:85]
Student [學號:001, 成績:90]
Student [學號:004, 成績:92]
Student [學號:005, 成績:99]
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态