我試圖做這個練習:我有一個學生有一個名字,姓氏和一個數字,我想通過數字命令學生..如果我想爲了通過名字或姓氏,似乎很容易,但用數字我不知道該怎麼做.. 這是我的代碼:JAVA如何在本練習中比較使用compareTo的整數
public class Student implements Comparable<Student> {
private String name;
private String surname;
private int number;
public Student(String n, String s, int m) {
name = n;
surname = s;
number = m;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public int getmatricola() {
return number;
}
//CompareTo Name
public int compareTo(Student otherObject) {
return name.compareTo(otherObject.getName());
}
}
//TESTER
ArrayList<Student> list = new ArrayList<Student>();
System.out.print("\n ORDER BY NUMBER \n");
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
String std = s.getAll();
System.out.println(std);
}
它實際上比較容易通過數字進行比較。 – Maroun 2015-02-07 10:03:31
['Integer.compare(int,int)'](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#compare(int,%20int))可以幫助 – 2015-02-07 10:04:59