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

时间:2019-05-02 01:49:48

标签: java algorithm file arraylist selection-sort

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

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

4 个答案:

答案 0 :(得分:0)

如果您声明一个具有两个字段Studentname的{​​{1}}类,这将很容易。您可以使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个学生(作为Student implements Comparable<Student>)。

答案 1 :(得分:0)

一旦您获得了学生名单。您可以使用比较器来做到这一点。

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());

答案 2 :(得分:0)

您可以创建一个学生对象,以分别保存姓名和等级。将所有数据添加到列表后,可以通过使用Comparator直接使用list.sort()方法,但是在这种情况下,您要编写选择排序,这就是为什么必须编写另一种方法进行选择排序的原因。 / p>

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 + "]";
    }

}

答案 3 :(得分:0)

在同一文件夹中创建一个单独的Student.java文件以容纳Student类:

with CASECODESTATS (ORDBATCH,PO,EARLIEST,OPERATOR,count1,count2) as 
(
select substr(code,1,15),substr(code,1,14),MIN(LSTUPDTIME),LSTMODIFIER,COUNT(c.code) ,0
from casecode c
where c.state='REVIEWED'
group by substr(code,1,15),LSTMODIFIER,substr(code,1,14)


union all 

select substr(code,1,15),substr(code,1,14),MIN(LSTUPDTIME),LSTMODIFIER,0,COUNT(c.code)
from casecode c
where c.state ='WAREHOUSE RECEIVE'
group by substr(code,1,15),LSTMODIFIER,substr(code,1,14)
 )

select ORDBATCH ORBATCH,sum(count1) REVIEWED ,sum(count2) WAREHOUSERECIEVE,MIN(EARLIEST) EARLIESTDATE,OPERATOR LSTMODIFIER,PO ORDERNUM from CASECODESTATS

where 1=1
group by ORDBATCH,OPERATOR,PO

order by ORDBATCH

然后按空格将每一行分开,并将第一个标记设置为名称,将最后一个标记设置为成绩,然后使用Comparator对学生对象的ArrayList进行排序:

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;
    }
}