2014-09-21 56 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 
    } 

} 
相關問題