读入输入流的N行并以相反的顺序打印而不使用数组或列表类型结构? [英] Read in N Lines of an Input Stream and print in reverse order without using array or list type structure?

查看:145
本文介绍了读入输入流的N行并以相反的顺序打印而不使用数组或列表类型结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 BufferedReader readLine()方法,可以反向打印流的前N行不使用列表或数组的顺序?

Using the readLine() method of BufferedReader, can you print the first N lines of a stream in reverse order without using a list or an array?

推荐答案

我认为你可以通过以下方式进行递归来实现:

I think you can do it through recursion with something like:

void printReversed(int n)
{
   String line = reader.readLine();

   if (n > 0)
     printReversed(n-1);

   System.out.println(line);
}

这篇关于读入输入流的N行并以相反的顺序打印而不使用数组或列表类型结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆