gpt4 book ai didi

java - 如何将包含字符串和整数的文件读入 ArrayList 并按整数排序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:13:00 26 4
gpt4 key购买 nike

我正在尝试从包含按行组织的姓名和成绩列表的文本文件中读取,即:

大卫·史密斯 84

苏珊 L 波特 100

...

然后将它们(按行)存储在 ArrayList 中,并使用选择排序算法按学生的成绩对 ArrayList 进行排序,但是我尝试了多种不同的编码方式,每次编辑似乎都会提示另一个错误(我对编程非常陌生)。这是我目前拥有的:

import java.io.*;
import java.util.*;

public class Grades {
private static void sort(ArrayList<String> list) {
int pFill;
int pTest;
int pSmallest;
String temp;
for (pFill = 0; pFill < list.size(); pFill++) {
pSmallest = pFill;
for (pTest = pFill + 1; pTest < list.size(); pTest++) {
if (pTest < pSmallest) {
pSmallest = pTest;
}
}
temp = list.get(pSmallest);
list.set(pSmallest, list.get(pFill));
list.set(pFill, temp);
}
}

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();
sort(list);
System.out.println(list);
} catch (IOException e){
System.out.println(e.getMessage());
}
}
}

最佳答案

您可以创建一个学生对象来分别保存姓名和成绩。将所有数据添加到列表后,您可以通过使用 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 + "]";
}

}

关于java - 如何将包含字符串和整数的文件读入 ArrayList 并按整数排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944714/

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