【发布时间】:2020-08-18 06:17:40
【问题描述】:
我正在尝试读取文本文件(文本行),使用 push 方法将所有元素放入堆栈。一旦我这样做了,我计划使用 pop 方法逐行打印所有元素。
- 一次读取一行输入,然后将这些行写入
- 倒序,使最后输入的行先打印,然后
- 倒数第二个输入行,以此类推。
class part1{
//pushing element on the top of the stack
static void stack_push(Stack<String> stack){
for(int i = 0; i< stack.length(); i++){
stack.push(i);
}
}
static void stack_pop(Stack<String> stack){
System.out.println("Pop :");
for(int i = 0; i < stack.length(); i++){
String y = (String) stack.pop();
System.out.println(y);
}
}
public static void main(String args[]){
BufferedReader br = new BufferedReader(new FileReader("randomFile.txt"));
stack_push(br);
}
}
【问题讨论】:
-
您的代码无法编译。您将
BufferedReader的对象传递给stack_push,但此方法没有任何匹配参数。 -
你的问题到底是什么?
-
我很困惑我应该如何将每一行从文本文件推送到堆栈
-
@bobbyverm - 阅读一些基本教程,如 docs.oracle.com/javase/tutorial/essential/io/file.html 以了解如何从文件中读取行。阅读时在堆栈中添加一行。
-
为什么将缓冲读取器作为 Stack
传递,为什么要推入这个堆栈整数?
标签: java data-structures stack