4 回答
TA贡献1802条经验 获得超5个赞
一旦你得到学生名单。您可以使用 Comparator 来执行此操作。
List<Student> sorted=list.sort(Comparator.comparing(p-> p.getGrade()));
或者使用流 api
List<Person> result =list.stream().sorted((p1, p2)>p1.getGrade().compareTo(p2.getGrade())).collect(Collectors.toList());
TA贡献1817条经验 获得超14个赞
在同一文件夹中创建一个单独的 Student.java 文件来保存 Student 类:
public class Student {
private final String name;
private final int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public String getName(){
return name;
}
public int getGrade(){
return grade;
}
}
然后用空格分割每一行并将第一个标记设置为名称,最后一个标记为等级,然后使用 Comparator 对学生对象的 ArrayList 进行排序:
import java.io.*;
import java.util.*;
public class Grades {
private static ArrayList<Student> sort(ArrayList<String> list) {
ArrayList<Student> students = new ArrayList<Student>();
String name = "";
int grade;
for (String line : list) {
String[] splitted = line.split("\\s+");
for(int i = 0;i< splitted.length-1;i++){
name += splitted[i] + " ";
}
grade = Integer.parseInt(splitted[splitted.length-1]);
students.add(new Student(name,grade));
name = "";
}
students.sort(Comparator.comparing(student-> student.getGrade()));
return students;
}
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
String fileName = "students.txt";
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {
while(input.ready()){
list.add(input.readLine());
}
input.close();
ArrayList<Student> sortedStudents = sort(list);
for (Student currentStudent : sortedStudents)
System.out.println(currentStudent.getName() + currentStudent.getGrade());
} catch (IOException e){
System.out.println(e.getMessage());
}
}
}
TA贡献1796条经验 获得超4个赞
如果您声明一个Student具有 2 个字段name和age. 并且您可以使Student班级实施Comparable并根据等级进行比较。它看起来像这样:
public class Student implements Comparable<Student> {
private final String name;
private final int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
@Override
public int compareTo(Student s) {
return Integer.compare(this.grade, grade);
}
}
要填充Student对象,您需要拆分String并提取名称和等级,然后调用new Student(name, grade).
在您的sort方法中,您可以通过调用类似的东西List<Student>来比较 2 个学生(as ) 。Student implements Comparable<Student>s1.compareTo(s2)
TA贡献1862条经验 获得超6个赞
您可以创建一个学生对象来分别保存姓名和成绩。将所有数据添加到列表后,您可以使用Comparator直接使用 list.sort() 方法,但在您的情况下,您想编写选择排序,这就是为什么您必须编写另一种方法来进行选择排序的原因。
package com.stackovflow.problems;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Grades {
public static void main(String[] args){
ArrayList<Student> list = new ArrayList<>();
String fileName = "students.txt";
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {
while(input.ready()){
String line = input.readLine().trim();
String name = line.substring(0,line.lastIndexOf(' '));
int grade = Integer.parseInt(line.substring(line.lastIndexOf(' ')+1));
list.add(new Student(name, grade));
}
input.close();
selectionSort(list);
System.out.println(list);
} catch (IOException e){
System.out.println(e.getMessage());
}
}
private static void selectionSort(ArrayList<Student> list) {
int pFill;
int pTest;
int pSmallest;
Student temp;
for (pFill = 0; pFill < list.size(); pFill++) {
pSmallest = pFill;
for (pTest = pFill + 1; pTest < list.size(); pTest++) {
Student pTestStudent = list.get(pTest);
Student pSmallestStudent = list.get(pSmallest);
if (pTestStudent.getGrade() < pSmallestStudent.getGrade()) {
pSmallest = pTest;
}
}
if(pSmallest!=pFill) {
temp = list.get(pSmallest);
list.set(pSmallest, list.get(pFill));
list.set(pFill, temp);
}
}
}
}
//This class is to hold line data in your students.txt file
class Student{
private String name;
private int grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student(String name, int grade) {
super();
this.name = name;
this.grade = grade;
}
@Override
public String toString() {
return "Student [name=" + name + ", grade=" + grade + "]";
}
}
添加回答
举报