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

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


大卫史密斯 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());

        }

    }

}


PIPIONE
浏览 102回答 4
4回答

慕后森

一旦你得到学生名单。您可以使用 Comparator 来执行此操作。List<Student>&nbsp;sorted=list.sort(Comparator.comparing(p->&nbsp;p.getGrade()));或者使用流 apiList<Person>&nbsp;result&nbsp;=list.stream().sorted((p1,&nbsp;p2)>p1.getGrade().compareTo(p2.getGrade())).collect(Collectors.toList());

大话西游666

在同一文件夹中创建一个单独的 Student.java 文件来保存 Student 类:public class Student {&nbsp; &nbsp; private final String name;&nbsp; &nbsp; private final int grade;&nbsp; &nbsp; public Student(String name, int grade) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.grade = grade;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName(){&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public int getGrade(){&nbsp; &nbsp; &nbsp; &nbsp; return grade;&nbsp; &nbsp; }}然后用空格分割每一行并将第一个标记设置为名称,最后一个标记为等级,然后使用 Comparator 对学生对象的 ArrayList 进行排序:import java.io.*;import java.util.*;public class Grades {&nbsp; &nbsp; private static ArrayList<Student> sort(ArrayList<String> list) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Student> students = new ArrayList<Student>();&nbsp; &nbsp; &nbsp; &nbsp; String name = "";&nbsp; &nbsp; &nbsp; &nbsp; int grade;&nbsp; &nbsp; &nbsp; &nbsp; for (String line : list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] splitted = line.split("\\s+");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i< splitted.length-1;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name += splitted[i] + " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grade = Integer.parseInt(splitted[splitted.length-1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students.add(new Student(name,grade));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; students.sort(Comparator.comparing(student-> student.getGrade()));&nbsp; &nbsp; &nbsp; &nbsp; return students;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; String fileName = "students.txt";&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(input.ready()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(input.readLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Student> sortedStudents = sort(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Student currentStudent : sortedStudents)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentStudent.getName() + currentStudent.getGrade());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕的地8271018

如果您声明一个Student具有 2 个字段name和age. 并且您可以使Student班级实施Comparable并根据等级进行比较。它看起来像这样:public class Student implements Comparable<Student> {&nbsp; &nbsp; private final String name;&nbsp; &nbsp; private final int grade;&nbsp; &nbsp; public Student(String name, int grade) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.grade = grade;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Student s) {&nbsp; &nbsp; &nbsp; &nbsp; return Integer.compare(this.grade, grade);&nbsp; &nbsp; }}要填充Student对象,您需要拆分String并提取名称和等级,然后调用new Student(name, grade).在您的sort方法中,您可以通过调用类似的东西List<Student>来比较 2 个学生(as ) 。Student implements Comparable<Student>s1.compareTo(s2)

阿波罗的战车

您可以创建一个学生对象来分别保存姓名和成绩。将所有数据添加到列表后,您可以使用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 {&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Student> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = "students.txt";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(input.ready()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = input.readLine().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = line.substring(0,line.lastIndexOf(' '));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int grade = Integer.parseInt(line.substring(line.lastIndexOf(' ')+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Student(name, grade));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectionSort(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private static void selectionSort(ArrayList<Student> list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pFill;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pTest;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pSmallest;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (pFill = 0; pFill < list.size(); pFill++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pSmallest = pFill;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (pTest = pFill + 1; pTest < list.size(); pTest++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student pTestStudent = list.get(pTest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Student pSmallestStudent = list.get(pSmallest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pTestStudent.getGrade() < pSmallestStudent.getGrade()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pSmallest = pTest;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(pSmallest!=pFill) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = list.get(pSmallest);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.set(pSmallest, list.get(pFill));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.set(pFill, temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }//This class is to hold line data in your students.txt fileclass Student{&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int grade;&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public int getGrade() {&nbsp; &nbsp; &nbsp; &nbsp; return grade;&nbsp; &nbsp; }&nbsp; &nbsp; public void setGrade(int grade) {&nbsp; &nbsp; &nbsp; &nbsp; this.grade = grade;&nbsp; &nbsp; }&nbsp; &nbsp; public Student(String name, int grade) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.grade = grade;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Student [name=" + name + ", grade=" + grade + "]";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java