JAVA 如何在本练习中使用 compareTo 比较整数
JAVA How to compare integer using compareTo in this exercise
我正在尝试做这个练习:我有一个学生有名字、姓氏和编号,我想按编号给学生排序。如果我想按名字或姓氏排序,这似乎很容易但有了数字我不知道该怎么做..
这是我的代码:
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);
}
那么为什么你的数字是一个整数你可以减去数字和 return 差值:
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 number
public int compareTo(Student otherObject) {
return number - otherObject.getmatricola();
}
}
你可以实现类似的东西:
public int compareTo(Student otherObject) {
return Integer.compare(this.number, otherObject.getNumber());
}
尝试这样做...应该有效
//比较名称
public int compareTo(Student otherObject) {
如果( this.getmatricola() > otherObject.getmatricola())
return 1;
别的
return-1;
}
我正在尝试做这个练习:我有一个学生有名字、姓氏和编号,我想按编号给学生排序。如果我想按名字或姓氏排序,这似乎很容易但有了数字我不知道该怎么做.. 这是我的代码:
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);
}
那么为什么你的数字是一个整数你可以减去数字和 return 差值:
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 number
public int compareTo(Student otherObject) {
return number - otherObject.getmatricola();
}
}
你可以实现类似的东西:
public int compareTo(Student otherObject) {
return Integer.compare(this.number, otherObject.getNumber());
}
尝试这样做...应该有效 //比较名称 public int compareTo(Student otherObject) { 如果( this.getmatricola() > otherObject.getmatricola()) return 1; 别的 return-1; }