
package method2;
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String args[]){
ArrayList list= new ArrayList();
list.add(new Score(1, "Liming",472));
list.add(new Score(2, "Fangfang",445));
list.add(new Score(3, "Lily",323));
list.add(new Score(4, "Green",540));
list.add(new Score(5, "Mike",332));
Collections.sort(list,new StuScoreComparator());
for(int i=0;i
}
Collections.binarySearch(list, new Score(1, "Liming",472));
}
}
class StuScoreComparator extends Score implements java.util.Comparator{
public int compare(Object o1,Object o2){
Score score1=(Score) o1;
Score score2=(Score) o2;
return score1.getStuScore()-score2.getStuScore();
}
}
package method2;
/**
*
* @author Liuzhichao
*/
public class Score {
private int stuNumber;
private String stuName;
private int stuScore;
Score(){
}
public Score(int stuNumber,String stuName,int stuScore){
if (stuNumber > 0) {
this.stuNumber = stuNumber;
}
else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
if (stuScore > 0) {
this.stuScore = stuScore;
}
else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
this.stuName=stuName;
}//设置学生信息
public void setStuNumber(int stuNumber){
this.stuNumber=stuNumber;
}//设置学生学号
public void setStuScore(int stuScore){
this.stuScore=stuScore;
}//设置学生分数
public void setStuName(String stuName){
this.stuName=stuName;
}//设置学生名字
public int getStuNumber(){
return this.stuNumber ;
}//设置学生学号
public int getStuScore(){
return this.stuScore ;
}//设置学生分数
public String getStuName(){
return this.stuName;
}//获取学生名字
public String getStuInfomation(){
return "名字:"+this.stuName+" 学号:"+this.stuNumber+" 成绩:"+this.stuScore;
}//获取学生信息
}
大家说的都没有错 但是怎么打印输出哦,这几个不太熟悉的类把我搞晕了,把所有学生的信息打印出来,再实现查询学生的信息的功能
o(∩_∩)o...,老师要求用对象数组的。所以才这样写
就在我的主程序test里面改吧
修改Score类声明,实现java.lang.Comparable接口;
public class Score implements java.lang.Comparable{
在方法getStuInfomation() 后追加两个方法(compareTo为Comparable接口要求实现此方法,toString为重写基类的打印输出方法):
public int compareTo(Object otherScore) {
return this.stuScore - ((Score) otherScore).stuScore;
}
public String toString() {
return "[学号:" + stuNumber + "分数:" + stuScore + "姓名:" + stuName + "洞胡]";
}
重新编译Score.java,再运行Test,输出结果为:
----------
[学号:3分数:323姓名:Lily]
[学号:5分数:332姓名:Mike]
[学号:2分数:445姓名:Fangfang]
[学号:1分数:472姓名:Liming]
[学号:4分数:540姓名:Green]
-->2008/04/05
哎......
package method2;
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add(new Score(1, "Liming", 472));
list.add(new Score(2, "Fangfang", 445));
list.add(new Score(3, "Lily", 323));
list.add(new Score(4, "Green", 540));
list.add(new Score(5, "Mike", 332));
Collections.sort(list, new StuScoreComparator());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
Collections.binarySearch(list, new Score(1, "Liming", 472));
}
}
class StuScoreComparator extends Score implements java.util.Comparator {
public int compare(Object o1, Object o2) {
Score score1 = (Score) o1;
Score score2 = (Score) o2;
return score1.getStuScore() - score2.getStuScore();
}
}
package method2;
/**
*
* @author Liuzhichao
*/
public class Score implements java.lang.Comparable {
private int stuNumber;
private String stuName;
private int stuScore;
Score() {
纳芦拦 }
public Score(int stuNumber, String stuName, int stuScore) {
if (stuNumber > 0) {
this.stuNumber = stuNumber;
} else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
if (stuScore > 0) {
this.stuScore = stuScore;
} else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
this.stuName = stuName;
}//设置学生信息
public void setStuNumber(int stuNumber) {
this.stuNumber = stuNumber;
}//设置学生学号
public 哗吵void setStuScore(int stuScore) {
this.stuScore = stuScore;
}//设置学生分数
public void setStuName(String stuName) {
this.stuName = stuName;
}//设置学生名字
public int getStuNumber() {
return this.stuNumber;
}//设置学生学号
public int getStuScore() {
return this.stuScore;
}//设置学生分数
public String getStuName() {
return this.stuName;
}//获取学生名字
public String getStuInfomation() {
return "名字:" + this.stuName + " 学号:" + this.stuNumber + " 成绩:" + this.stuScore;
}//获取学生信息
public int compareTo(Object otherScore) {
return this.stuScore - ((Score) otherScore).stuScore;
}
public String toString() {
return "[学号:" + stuNumber + "分数:" + stuScore + "姓名:" + stuName + "]";
}
}
你对java 思想理解得乱源不错 基本的操作可以吧
不过哗茄态你把每个学生信息都封装成一个对象,数据量如果大的话就有性能这么纳让一说了。
如果你只想排序,那么你的做法很正确。但是如果你想使用Collections.binarySearch()方法进行搜索的话,那么list中的对象就需要实现Comparable接口。
下面是我修改后的程序,仅供参考:
--------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String args[]) {
ArrayList
list.add(new Score(1, "Liming", 472));
list.add(new Score(2, "Fangfang", 445));
list.add(new Score(3, "Lily", 323));
list.add(new Score(4, "Green", 540));
list.add(new Score(5, "Mike", 332));
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
int n = Collections.binarySearch(list, new Score(1, "Liming", 472));
System.out.println("You found: " + list.get(n));
}
}
/**
*
* @author Liuzhichao
*/
class Score implements Comparable
private int stuNumber;
private String stuName;
private int stuScore;
Score() {
}
public Score(int stuNumber, String stuName, int stuScore) {
if (stuNumber > 0) {
this.stuNumber = stuNumber;
} else {
System.out.println("输入的学生学号不信裂灶正确");
System.exit(1);
}
if (stuScore > 0) {
this.stuScore = stuScore;
} else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
this.stuName = stuName;
}// 设置学生信息
public void setStuNumber(int stuNumber) {
this.stuNumber = stuNumber;
}// 设置学生学号
public void setStuScore(int stuScore) {
this.stuScore = stuScore;
}// 设置学生分数
public void setStuName(String stuName) {
this.stuName = stuName;
}// 设置学生名字
public int getStuNumber() {
return this.stuNumber;
}// 设置学生学号
public int getStuScore() {
return this.stuScore;
}// 设置学生分数
public String getStuName() {
return this.stuName;
}// 获取学生滑扮名字
// 获取学生信息
public String toString() {
return "名字:" + this.stuName + " 学源皮号:" + this.stuNumber + " 成绩:"
+ this.stuScore;
}
public int compareTo(Score otherScore) {
return this.stuScore - otherScore.stuScore ;
}
}
我看到就晕晕了。。我还是初学者。。