gpt4 book ai didi

java - 按其整数内容对 arrayList 进行排序

转载 作者:行者123 更新时间:2023-12-01 13:49:42 26 4
gpt4 key购买 nike

我将一个 arrayList 传递给一个方法来对其数据进行排序。arrayList 中的数据将包含与不同学生相关的多个 ID 号。该数组还将包含其他信息。我如何按 id # 对 arrayList 进行排序。我相信这与我创建的类(class)有关,如下所示根据需要可以提供更多代码

public static class Student {

public String firstName;
public String lastName;
public Integer uid;
public StudentType type;

public Student(Student orig) {
this.firstName = orig.firstName;
this.lastName = orig.lastName;
this.uid = orig.uid;
this.type = orig.type;
}

// construct a new student with given fields
public Student(String firstName, String lastName, Integer newUid, StudentType type) {
this.firstName = firstName;
this.lastName = lastName;
this.uid = newUid;
this.type = type;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}
//set type

public void setType(StudentType type) {
this.type = type;
}
//return type

public StudentType getType() {
return type;
}
public void setUid(Integer uid){
this.uid = uid;
}
public Integer getUid() {
return uid;
}

// return a string representation of the invoking object
public String toString() {
return firstName + " " + lastName + " " + uid + " " + type;
}

public static class Graduate extends Student {

public boolean thesis;
public ClassStanding study;
public String profName;

public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
super(orig);
thesis = isThesis;
this.study = study;
this.profName = profName;
}

public boolean getThesis() {
return thesis;
}

public void setThesis(Boolean thesis) {
this.thesis = thesis;
}

public ClassStanding getStudy() {
return study;
}

public void setStudy(ClassStanding study) {
this.study = study;
}

public String getProfName() {
return profName;
}

public void setProfName(String profName) {
this.profName = profName;
}

public String toString() {
return super.toString() + thesis + " " + study + " " + profName;
}
}

public static class UnderGraduate extends Student {

public Major major;
public Double overallGpa;
public Double majorGpa;
public ClassStanding study;

public UnderGraduate(Student orig, Major major, Double overallGpa, Double majorGpa, ClassStanding study) {
super(orig);
this.study = study;
this.major = major;
this.overallGpa = overallGpa;
this.majorGpa = majorGpa;
}

public void setMajor(Major major) {
this.major = major;
}
//return type

public Major getmMajor() {
return major;
}

public void setOverallGPA(Double overallGpa) {
this.overallGpa = overallGpa;
}

public Double getOverallGPA() {
return overallGpa;
}

public void setMajorGPA(Double majorGpa) {
this.majorGpa = majorGpa;
}

public Double getMajorGPA() {
return majorGpa;
}

public ClassStanding getStudy() {
return study;
}

public void setStudy(ClassStanding study) {
this.study = study;
}

public String toString() {
return study + " " + major + " " + overallGpa + " " + majorGpa;
}
}
}

最佳答案

Collections.sort(myListofStudents, new Comparator<Student>() {
@Override
public int compareTo(Student s1, Student s2) {
return s1.getUid().compareTo(s2.getUid());
}
});

或者,您可以拥有 Student实现Comparable<Student> ,这意味着包括 .compareTo()

注意:如果您要覆盖 .compareTo() ,您应该覆盖 .equals() ,这意味着您应该覆盖 hashCode() .

关于java - 按其整数内容对 arrayList<class> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com