2014-09-21 81 views
1

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

我能够弄清楚如何以相反的方式打印第一个项目,但是我遇到了一些麻烦,让我继续离开原始数组。

我也只允许同时存储21个项目。

这里是我到目前为止(50items而不是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); 
      } 
     } 
    } 
} 

我在想,如果有人能帮助我,不知道我可以在这里做。

回答

1

首先,只要counter达到20的倍数以及碰到max,就需要打印列表。然后,您打印的s内容后,清除列表:

s.clear(); 

,将删除所有的元素,因此它会再次填满。您还需要在for循环退出后打印列表,否则最后几个项目将保留未打印。

请注意,您未在此代码中的任何位置使用数组。目前尚不清楚您是否使用LinkedList来遵守作业的精神。但只有你知道这个标题是什么。

+0

谢谢你这么简单s.clear(); 我能够修复它,并完成它。 – johnnyboyyy 2014-09-21 23:41:19

0

对于这部分的质询:

有人告诉我要打印第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,尽管实施了针对LinkedList的Java实现,使得其允许StackLIFO)行为。它通常只用作QueueFIFO)结构。我的答案也会使用LinkedList以使其与您的方法一致,但在未来的这种情况下考虑Stack

所以,既然您是从文件中读取数字,一行行,这里是你可以做什么:

  • 你可以阅读,并在LinkedList的顶端插入数字,直到你到达max值或文件的末尾

    你已经从顶部移除它们有一部分工作

  • 打印所有号码这将使它们以相反的顺序

    您打印他们,但没有将其删除或致电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()); 
    } 
} 
0

我希望这可以让你开始:

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 
    } 

}