我正在尝试编写一个方法,该方法将获取Student对象的ArrayList,并返回一个String数组,其中包含学生的分数顺序(具有最高分数的学生姓名将在索引0处)。 / p>
public static void orderStudent(List<Student> ls) {
for (Student stu : ls) {
System.out.println("Name: " + stu.getName() + ", Score: "
+ stu.getScore());
}
}
执行上面的代码片段会打印类似
的内容Name: Alex, Score: 10.35
Name: Bob, Score: 11.2
Name: Charles, Score: 8.22
我希望orderStudent方法返回一个String数组,该数组的内容[Bob,Alex,Charles] Bob是Alex和Charles的最佳得分手。
答案 0 :(得分:2)
如果Student
实现Comparable
接口并按分数排序,则只需对列表进行排序,然后构建名称数组。
如果您无法编辑Student
类,则需要编写一个实现Comparator<Student>
的类,并使用该类对列表进行排序,然后构建您的名称数组。
简化@ PrasadKhode的回答:
public static String[] orderStudent(List<Student> list) {
String[] students = new String[list.size()];
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student object1, Student object2) {
return Integer.compare(object2.getScore(), object1.getScore());
}
});
for (int index = 0; index < list.size(); index++) {
Student student = list.get(index);
students[index] = student.getName();
System.out.println("Name: " + student.getName());
}
return students;
}
对于每个分数比较,无需为CompareToBuilder
创建实例。那效率很低。
答案 1 :(得分:2)
首先,您需要对List进行排序,然后需要构造String []
您可以使用CompareToBuilder
课程,而无需对现有的POJO 课程进行任何更改。
在CompareToBuilder上,您需要添加需要对集合进行排序的属性。您可以看到以下代码:
import org.apache.commons.lang3.builder.CompareToBuilder;
...
public static String[] orderStudent(List<Student> list) {
String[] students = new String[list.size()];
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student object1, Student object2) {
return new CompareToBuilder().append(object2.getScore(), object1.getScore()).toComparison();
}
});
for (int index = 0; index < list.size(); index++) {
Student student = list.get(index);
students[index] = student.getName();
System.out.println("Name: " + student.getName());
}
return students;
}
答案 2 :(得分:1)
您需要使用按分数(必填)
排序的自定义组合器对对象进行排序Collections.sort(列表列表,比较器c)
遍历已排序的集合并打印
答案 3 :(得分:1)
您需要在Student
班级Comparable
中实施2项内容,并覆盖.toString()
方法。首先,定义你的学生班:
public class Student implements Comparable<Student>
然后您必须按如下方式定义.compareTo()
方法:
//used for sorting later
public int compareTo(Student other) {
return this.getScore().compareTo(other.getScore());//Assuming Student.score is not a primitive type
}
同样覆盖.toString()
方法,如下所示:
//used for printing later
public string toString() {
return "Name: " + this.getName() + ", Score: " + this.getScore().toString();
}
现在您只需将orderStudents
功能更改为以下内容:
public static void orderStudent(List<Student> ls) {
// this will sort your collection based on the logic in the `Student.compareTo()` method.
ls = Collections.sort(ls);
for (Student stu : ls) {
//this will print the student object based on the `Student.toString()` method
System.out.println(stu);
}
}
答案 4 :(得分:1)
用列表替换数组:
public static List<String> orderStudent(List<Student> ls) {
Collections.sort(ls, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore().compareTo(o1.getScore());
}
});
List<String> result = new ArrayList<String>();
for(Student s : ls) {
result.add(s.getName());
}
return result;
}
如果使用Java 8,它将少于两行...