【问题标题】:Output 22 lines previously read - Java输出之前读取的 22 行 - Java
【发布时间】:2013-09-22 22:35:08
【问题描述】:

我正在尝试从文件中读取,当读取一个空行时,它将在空行之前输出第 22 行。例如,如果程序在第 44 行读取第一个空白行的所有行,那么它将打印第 22 行。

目前我让它工作,以便它读取输入并将其存储到 arrayList 中,然后输出它。我想知道最有效的方法是什么?我还试图确保一次存储的行数不超过 23 行。 arraylist 是适合这个的数据结构吗?

public static void test(BufferedReader r, PrintWriter w) throws IOException {

    ArrayList<String> s = new ArrayList<String>();

    String line;
    int n = 0;
    while ((line = r.readLine()) != null) {
        s.add(line);
        n++;
    }
    Iterator<String> i = s.iterator();
    while (i.hasNext()) {
        w.println(i.next());
    }
}

感谢您的任何建议/意见

【问题讨论】:

    标签: java data-structures arraylist


    【解决方案1】:

    您可以使用大小为 22 的简单 String[] 并执行插入和“获取”模 22。
    您的代码应如下所示:

    public static void test(BufferedReader r, PrintWriter w) throws IOException {
    
        String[] prev22 = new String[22];
        String line;
        int n = 0;
        while ((line = r.readLine()) != null) {
            prev22[n % 22] = line;
            if(line.trim() == null){
                w.println(prev22[n-22 % 22]);
            }
            else{
                w.println(line);
            }
            n++;
        }
    }
    

    【讨论】:

    • 有什么数据结构可以用来做这个吗?
    • @choloboy 你不需要任何特殊的数据结构,一个简单的长度为 22 的字符串数组就可以了。
    • 任何一种循环数组数据结构都足够了。当前的大多数答案都显示了快速实施。在线也有实现,例如museful.net/2012/software-development/…
    【解决方案2】:

    ArrayList 有点矫枉过正。由于您确切知道总行数,因此您可以使用字符串数组。

    但是,您必须保留有关什么是当前“开始指针”的信息(如果您想保留顺序并以有效的循环方式使用它。

    示例:使用自定义循环数组

     public class CircularStringArray {
    
        private int currenInsertIndex = 0;
    
        private String[] array = new String[22];
    
        public void addString(String element)
        {
            array[currenInsertIndex++] = element;
            currenInsertIndex = currenInsertIndex % array.length;
        }   
    
        public String printStrings()
        {
            String result = "";
            for(int i=currenInsertIndex; i<array.length; i++)
            result+=i+")"+array[i]+"\n";
    
            for(int i=0; i<currenInsertIndex; i++)
            result+=i+")"+array[i]+"\n";
    
            return result;
        }
    
        public static void main(String args[])
        {
            CircularStringArray test = new CircularStringArray();
            for (int i=0; i<50; i++)
            test.addString(new String(new char[]{(char)i}));
    
            System.out.println(test.printStrings());            
        }
    }
    

    【讨论】:

    • 为什么不将这个% SIZE 逻辑也用于printStrings() 方法。??
    • 好点.. 虽然我怀疑在效率方面这更有效,因为使用模数会产生额外的成本循环。
    【解决方案3】:

    ArrayList 将存储所有行,直到空白。您可以使用大小为 22 的字符串的 Queue。队列的 enqueuedequeue 操作需要恒定的 O(1) 时间,并且内存使用量也将是最小的

    【讨论】:

      【解决方案4】:

      这应该可以工作

      public static void test(BufferedReader r, PrintWriter w) throws IOException {
      
         String[] s = new String[22];
      
         String line;
         int n = 0;
         while ((line = r.readLine()) != null) {
             s[n] = line;
              //This will take n to 0 when n is 21 i.e. last array position
             n = (n+1) % 22;
         }
         int last = n;
      
         //Will print it in the same order in which those lines were read.
         do{
               w.println(s[n]);
               n = (n+1) % 22;
           } while(n != last);
      }
      

      【讨论】:

      • 有我可以使用的数据结构吗?
      • 如果现有的 DS 可以有效地解决它,为什么要使用 nes 数据结构。
      猜你喜欢
      • 2018-09-07
      • 2012-06-14
      • 1970-01-01
      • 2011-12-30
      • 2015-04-17
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多