【问题标题】:Print first 20 items in array in reverse, then print the next 20 in reverse, and so on反向打印数组中的前 20 个项目,然后反向打印接下来的 20 个项目,依此类推
【发布时间】:2014-09-21 20:44:33
【问题描述】:

所以我正在处理我的 Java 作业,我得到了一个大数组。 我被告知以相反的顺序打印数组中的前 20 个项目, 然后再次以相反的顺序打印接下来的 20 个项目,依此类推,直到我到达数组的末尾。

我能够弄清楚如何反向打印第一个项目,但是我无法实现一些可以让我继续从原始数组离开的地方的东西。

我一次只能存储 21 件物品。

这是我目前拥有的(50 件而不是 20 件)

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    LinkedList<String> s = new LinkedList<String>();
    int counter = 0;
    int max = 50;

    for (String line = r.readLine(); line != null; line = r.readLine()) {
        if (counter < max) {
            s.addFirst(line);
            counter++;
        }

        if (counter == max) {
            for (String n : s) {
                System.out.println(n);
            }
        }
    }
}

我想知道是否有人可以帮助我,不知道我能从这里做什么。

【问题讨论】:

    标签: java arrays printing reverse


    【解决方案1】:

    首先,您需要在counter 达到 20 的倍数以及达到 max 时打印该列表。然后,打印完s的内容后,清空列表:

    s.clear();
    

    这将删除所有元素,以便再次填充。您还需要在for 循环退出后打印列表,否则最后几项将不打印。

    请注意,您在此代码中的任何地方都没有使用数组。不清楚您是否通过使用LinkedList 来遵循作业的精神。但只有你知道评分标准是什么。

    【讨论】:

    • 感谢您的简单 s.clear();我能够修复它并完成它。
    【解决方案2】:

    我希望这可以帮助您入门:

    void example() {
    
        for (int i = 0; i < 50; i++) { //fill array to be read (for this example)
            myArray[i] = i;
        }
        readback();
    
    
    }
    
     void readback() {
    
        int batch = 1; //represents a portion of the read operation
        int batchSize = 20; //the size of the read portion
        int pos = 0; //the current index of the array while it is being read
        int hi; //the top of the batch
        int lo; //the bottom of the batch
    
    
        while (pos < myArray.length) {
    
            if (batch*batchSize<myArray.length) { //make sure you are not going over the array boundary
                 hi =  batch*batchSize;
                 lo = hi - batchSize;
            } else {
                hi = myArray.length;
                lo = pos;
            }
    
            for (int i = hi - 1; i >= lo; i--) { //read
                System.out.println(myArray[i]);
                pos++;
            }
            batch++; //go to the next batch
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      对于这部分问题:

      我被告知以相反的顺序打印数组中的前 20 个项目,然后再次以相反的顺序打印接下来的 20 个项目,依此类推,直到我到达数组的末尾。

      一个简单的解决方案是:

      • 在数组中迭代 20 个位置
      • 将此位置存储在临时索引中
      • 迭代返回 20 个位置打印数组
      • 从存储的临时索引重复过程

      另外,请注意最后一次打印的元素是否少于 20 个。

      int size = 20; // size of reversed chunks
      
      for(int i = 0; i < array.length; i += size) {
          int j = (i + (size - 1) < array.length) ? (i + size - 1) : array.length - 1;
          for(; j >= i; j--) {
              System.out.print(array[j] + " ");
          }
      }
      

      但是,您的代码中没有数组,所以我不确定您的意思。您正在从文件中读取值,然后使用 LinkedList 反向打印它们。一个更好、更自然的数据结构,用于反向打印(以及大多数“反转”操作)将是 Stack,尽管 Java 对 LinkedList 的实现是这样实现的,它允许 StackLIFO ) 行为。它只是一般用作Queue (FIFO) 结构。我的回答也将使用LinkedList 以使其与您的方法保持一致,但在未来这种情况下考虑使用Stack

      因此,由于您正在从文件中逐行读取数字,因此您可以执行以下操作:

      • 您可以读取并插入LinkedList 顶部的数字,直到到达max 值或文件末尾

        你已经让那部分工作了

      • 通过从LinkedList 的顶部删除它们来打印所有数字,这将使它们以相反的顺序排列

        您正在打印它们,但没有通过调用s.clear()删除它们或清除列表@

      • 一旦到达文件末尾,您最终可能会在 LinkedList 中保留值,因为您在到达 max 项之前到达文件末尾,并且循环完成但没有打印任何内容。也打印这些值。

      另一件事,您似乎没有写入文件,因此您不需要函数的PrintWriter 参数。

      代码如下:

      public static void doIt(BufferedReader r) throws IOException {
          LinkedList<String> s = new LinkedList<String>();
          int counter = 0;
          int max = 50;
      
          for (String line = r.readLine(); line != null; line = r.readLine()) {
              if (counter < max) {
                  s.addFirst(line);
                  counter++;
              }
      
              if (counter == max) {
                  while(!s.isEmpty()) { // remove and print in reverse order
                      System.out.println(s.removeFirst());
                  }
                  counter = 0; // reset counter
              }
          }
      
          // print the remaining elements, if they exist
          while(!s.isEmpty()) { // remove and print in reverse order
                  System.out.println(s.removeFirst());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 2022-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多