gpt4 book ai didi

java容器对象交换数组方法

转载 作者:行者123 更新时间:2023-12-02 11:10:15 24 4
gpt4 key购买 nike

我有 2 个类(class),一个是 Grades,它是一个容器类(class)。另一种是等级,它是一个对象等级。我正在按成绩和姓名对它们进行排序,成绩已从最高到最低完成,现在我尝试按姓名排序(如果成绩相同)。我使用交换方法,但无法交换容器类中的值。我只允许在驱动器内执行此操作,在两个类中都不允许进行编辑。请给我讲课。

我的代码:

      for(int i = 0 ; i < (sortedGrades.size()*sortedGrades.size()) ; i++){
for(int j = 0 ; j < sortedGrades.size() ; j++){
Grade a = sortedGrades.get(i);
Grade b = sortedGrades.get(i+1);
if(a.isSameGPA(b))
{
if(repeat.contains(a) == true &&
repeat.contains(b) == false)
{
Grade temp = a;
a = b;
b = temp;

sortedGrades.get(i);
}
}
}
}

结果应该是这样的:

S005: Stacy, Lu 4.0
S004: Aseef, Hernandez 3.9
S006: Aseef, Nilkund 3.9
S002: Jim, NLN 3.9
S003: Misty, Fang 3.9
S009: Steve, Calderon 3.9
S016: Aseef, Simmons 3.9
S010: Raj, Singh 3.8
S018: Hamza, Nilkund 3.5
S012: Kathy, Calderon 3.5
S017: Hifza, Nilkund 3.3
S011: Jason, Kramer 3.3
S001: John, Rodgers 3.3
S019: Chris, Peach 3.2
S013: Roopa, Singh 3.2
S020: Ramona, Luke 2.4
S014: Amid, Naveed 2.4
S015: Faith, Williams 1.0

最佳答案

如果您只想先按 double Value 然后按 String Value 对列表进行排序,您可以这样做:

(将我的代码中的对象更改为您的代码中的对象:Person = Grade,persons = SortedGrades)

Java 8 风格:

public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person(4.0, "Name-3"));
persons.add(new Person(3.9, "Name-1"));
persons.add(new Person(2.0, "Name-6"));
persons.add(new Person(2.0, "Name-4"));
persons.add(new Person(1.8, "Name-5"));
persons.add(new Person(1.3, "Name-7"));
persons.add(new Person(1.3, "Name-2"));
persons.add(new Person(1.0, "Name-8"));
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
System.out.println("-------------------------");
Comparator<Person> comparator = Comparator.comparing(Person::getValue).reversed().thenComparing(Person::getName);
persons.sort(comparator);
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
}

要运行此测试方法,您需要以下人员对象类:

public class Person {
private double value;
private String name;
public Person(double value, String name) {
super();
this.value = value;
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

或者您可以使用 Collections.sort 方法和自定义比较器来完成此操作:

public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person(4.0, "Name-3"));
persons.add(new Person(3.9, "Name-1"));
persons.add(new Person(2.0, "Name-6"));
persons.add(new Person(2.0, "Name-4"));
persons.add(new Person(1.8, "Name-5"));
persons.add(new Person(1.3, "Name-7"));
persons.add(new Person(1.3, "Name-2"));
persons.add(new Person(1.0, "Name-8"));
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
System.out.println("-------------------------");

Collections.sort(persons, new Comparator<Person>() {

public int compare(Person p1, Person p2) {
Double v1 = p1.getValue();
Double v2 = p2.getValue();
int vComp = v2.compareTo(v1);
if (vComp != 0) {
return vComp;
}
String n1 = p1.getName();
String n2 = p2.getName();
return n1.compareTo(n2);
}});

for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
}

关于java容器对象交换数组方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50662892/

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