【问题标题】:reading lines in from a text file and sorting them into a linked list java从文本文件中读取行并将它们排序到链表中 java
【发布时间】:2013-09-23 11:42:11
【问题描述】:

我需要从一个文本文件中读取每一行,并首先根据长度对其进行排序,然后是它在原始文档中的位置,然后再将这些行添加到链表中。

然后必须逐行打印列表的内容,并带有一个前缀,指示要打印的行号以及该行上有多少非空格字符。

下面是一个示例 I/O:

Input (i.e. contents of text file)

this
is
just
a
test

Output

1/1: a
2/2: is
3/4: this
4/4: just
5/4: test

【问题讨论】:

    标签: java sorting file-io linked-list comparable


    【解决方案1】:

    您需要使用文件和扫描仪。代码看起来像这样:

    import java.io.*;
    import java.util.scanner;
    
    public class ReadAndWrite {
        public static void main(String[] args) throws IOException {
    
            Scanner scan = new Scanner(new File("yourfile.txt"));
    
            int i = 1;
            while(scan.hasNext()) {
                String s = scan.nextLine();
                System.out.println("Line " + i + " says " + s + " and has " + s.length() + " characters.";
                i++;
            }
            System.out.println("/nNo more lines in the file.");
        }
    }
    

    【讨论】:

      【解决方案2】:
      1. 我需要从文本文件中读取每一行:使用 FileReader 和 BufferedReader
      2. 先按照长度排序,再按照在原始文档中的位置排序,再将行添加到链表中:使用原始文档的(String,lineNo)创建一个HashMap。
      3. 使用 Comparator 进行排序 - 首先按长度,然后使用三元运算符按行 pos(从 hashMap 中获取)。

      4. 行上有多少个非空格字符:使用 "s+" 分割行。使用 for 循环添加所有子数组的长度。

      5. 从arraylist打印时,打印count + nonSpaceChars in line + line。

      希望这就足够了

      【讨论】:

        【解决方案3】:

        我不会为您解决问题,而是会为您提供各种链接,以帮助您解决任务。

        1) Readinga file in JAVA

        2) 可以对读取的字符串执行各种字符串操作:String operations

        3) 在 JAVA 中使用比较器对集合进行排序:Collection sorting

        【讨论】:

        • 谢谢,这是我一直在寻找的建议!
        【解决方案4】:
        import java.util.*;
        import java.io.*;
        
        public class HelloWorld{
        
        public static class mystruct {
            public String line;
            public int    number;
            public mystruct(String line, int count) {
                this.line = line;
                this.number = count;
            }
        }
        
          public static void main(String []args){
             LinkedList<mystruct> list = new LinkedList<mystruct>();
             mystruct  temp;
             int count=0;
              try{
                  FileInputStream fstream = new FileInputStream("input.txt");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                  String readline;
                  while ((readline = br.readLine()) != null) {
                      count++;
                      temp = new mystruct(readline, count);
                    list.add(temp);
                  }
                  in.close();
                 } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                 }      
                 Collections.sort(list, new Comparator<mystruct>() {
                     public int compare(mystruct o1, mystruct o2) {
                         if (o1.line.length() != o2.line.length())
                            return (o1.line.length() - o2.line.length());
                        else {
                            return (o1.number - o2.number);
                        }
                     }
                 });
                  for (int i = 0; i < list.size(); i++) {
                    temp = list.get(i);
                    System.out.println(temp.line);
                }      
             }
        }
        

        【讨论】:

        • 请不要对文本使用 DataInputStream。
        猜你喜欢
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 2018-05-16
        • 1970-01-01
        相关资源
        最近更新 更多