通过比较线进行排序

时间:2013-11-25 07:39:39

标签: java sorting

此数据保存在data.txt中我正在尝试编写可以安排的程序

18b0885  // this is the registration number, 
   SS844 Parallel Algorithms        //  These are course taken by student
   SS555 Calculus for Distributed Computing
   SS501 Quantum Communication

17b0585
   SS828 Problem Based Programming
   SS660 Genetic Computation
   SS567 Hacking Quantum Network

17b2582
   SS567 Hacking Quantum Network
   SS876 Positronics
   SS880 Quark-based Logic

按照注册号码进行程序短路。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorter {

    static List<Data> read() throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
        List<Data> list = new ArrayList<>();
        String line;
        Data data = null;
        while ((line = reader.readLine()) != null) {
            if (line.matches("\\s*")) {
                continue; // skip blank lines
            }
            // Assume line that begins with space is a course.
            if (Character.isSpaceChar(line.charAt(0))) {
                //  Add new course to data if it's there.
                if (data == null) {
                    System.err.println("Missing serial for course " + line);
                } else {
                    data.courses.add(line);
                }
            } else {
                // Add completed data to list if there is one.
                if (data != null) {
                    list.add(data);
                }
                // Make new data with this serial.
                data = new Data(line);
            }
        }
        // Add the last data to the list if there is one.
        if (data != null) {
            list.add(data);
        }
        return list;
    }

    public static void main(String[] args) {
        try {
            // Read.
            List<Data> list = read();

            // Sort based on serials.
            Collections.sort(list, new Comparator<Data>() {
                @Override
                public int compare(Data a, Data b) {
                    return a.serial.compareTo(b.serial);
                }
            });

            // Print.
            for (Data data : list) {
                data.print();
            }
        } catch (Exception ex) {
            System.err.println("Read failed.");
        }
    }
}

// Local class to hold data: a serial and related courses.
class Data {

    String serial;
    List<String> courses;

    Data(String serial) {
        this.serial = serial;
        courses = new ArrayList<>();
    }

    void print() {
        System.out.println(serial);
        for (String course : courses) {
            System.out.println(course);
        }
    }
}

程序的输出如下

17b2582
   SS567 Hacking Quantum Network
   SS876 Positronics
   SS880 Quark-based Logic

17b0585
   SS828 Problem Based Programming
   SS660 Genetic Computation
   SS567 Hacking Quantum Network

18b0885  
   SS844 Parallel Algorithms       
   SS555 Calculus for Distributed Computing
   SS501 Quantum Communication

通缉输出如下。

SS501 Quantum Communication
     18b0885
     17b2582

SS567 Hacking Quantum Network
     17b2582
     17b0585

SS844 Parallel Algorithms 
     17b2582
     17b0585
     18b0885

我修改了代码并提供了这样的输出

    SS501 Quantum Communication
    SS555 Calculus for Distributed Computing
    SS567 Hacking Quantum Network
    SS567 Hacking Quantum Network
    SS660 Genetic Computation
    SS828 Problem Based Programming
    SS844 Parallel Algorithms
    SS876 Positronics
    SS880 Quark-based Logic

    17b2582
    17b0585
    18b0885

1 个答案:

答案 0 :(得分:1)

您可以简单地使用基于Map的实现,恕我直言代码会更简单,map直接符合您的要求:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Sorter {

    static Map<String, List<String>> read() throws FileNotFoundException,
            IOException {
        BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
        String line;
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        String value = "";
        while ((line = reader.readLine()) != null) {
            if (line.matches("\\s*")) {
                continue; // skip blank lines
            }
            // Assume line that begins with space is a course.
            if (Character.isSpaceChar(line.charAt(0))) {
                // Add new course to data if it's there.
                if (!map.containsKey(line)) {
                    map.put(line, new ArrayList<String>());
                }
                map.get(line).add(value);
            } else {
                // Add completed data to list if there is one.
                value = line;
            }
        }
        return map;
    }

    public static void main(String[] args) {
        try {
            Map<String, List<String>> list = read();
            Set<Entry<String, List<String>>> set = list.entrySet();
            for (Entry<String, List<String>> e : set) {
                System.out.println(e.getKey());
                for (String s : e.getValue()) {
                    System.out.println("\t" + s);
                }
            }
        } catch (Exception ex) {
            System.err.println("Read failed.");
        }
    }
}

基于发布的输入data.txt的示例输出应为:

SS828 Problem Based Programming
    17b0585
   SS555 Calculus for Distributed Computing
    18b0885  
   SS844 Parallel Algorithms        
    18b0885  
   SS876 Positronics
    17b2582
   SS660 Genetic Computation
    17b0585
   SS567 Hacking Quantum Network
    17b0585
    17b2582
   SS880 Quark-based Logic
    17b2582
   SS501 Quantum Communication
    18b0885