0

以下例程输出从国际象棋引擎移动到 JTextArea

public void getEngineOutputOriginal(Process engine) 
{
    try {           
        BufferedReader reader =
          new BufferedReader (new InputStreamReader (engine.getInputStream ()), 1);
        String lineRead = null;
        // send engine analysis to print method
        while ((lineRead = reader.readLine ()) != null)
           Application.showEngineAnalysis (lineRead);
    }
    catch (Exception e) {
                      e.printStackTrace();
    }
}

样本输出将是

           12     3.49  39/40?  2. b4      (656Knps)             
           12     3.49  40/40?  2. Nd5     (656Knps)             
           12->   3.51   0.04   2. Bxf4 Be6 3. Be3 Qa5 4. Nd5 Qxd2
           13     3.51   1/40?  2. Bxf4    (655Knps)   

是否可以反转该过程,以便读取的最后一行始终出现在顶部而不是底部,如下所示:

           13     3.51   1/40?  2. Bxf4    (655Knps)   
           12->   3.51   0.04   2. Bxf4 Be6 3. Be3 Qa5 4. Nd5 Qxd2
           12     3.49  40/40?  2. Nd5     (656Knps)             
           12     3.49  39/40?  2. b4      (656Knps)     

我研究了谷歌,但找不到解决方案

4

1 回答 1

2

当然!一种选择是将这些行缓冲到一个ArrayList中,然后在最后以相反的顺序显示它们:

List<String> lines = new ArrayList<String>();

/* Add all lines from the file to the buffer. */
while((lineRead = reader.readLine()) != null) {
    lines.add(lineRead);
}

/* Replay them in reverse order. */
for (int i = lines.size() - 1; i >= 0; i--) {
     Application.showEngineAnalysis(lines.get(i));
}

从概念上讲,您可以将其视为创建一个堆栈,将所有行压入堆栈,然后一次将它们弹出一个。

希望这可以帮助!

于 2012-06-11T21:20:18.133 回答