英文:
Reading in textfile into Stack and print out elements in reverse order
问题
我试图读取一个文本文件(一行一行的文本),使用push方法将所有元素放入一个栈中。一旦我完成这个步骤,我计划使用pop方法逐行打印所有元素。
- 逐行读取输入,然后以相反的顺序将这些行写出,这样最后输入的行会被首先打印,然后是倒数第二行输入,依此类推。
class part1{
//将元素推入栈顶
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("弹出:");
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);
}
}
英文:
I'm trying to read in a text file (lines of text), putting all elements into a stack using the push method. Once I do that I plan on printing all elements - line by line using the pop method.
- Read input one line at a time and then write the lines out in
- reverse order, so that the last input line is printed first, then
- the second last input line, and so on.
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);
}
}
答案1
得分: 0
以下是代码的翻译部分:
迭代("经典")读取文件并将其内容推入栈的方法:
public static void main(String args[]) {
String fileName = "randomFile.txt";
// 创建一个缓冲读取器
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
// 创建一个栈(注意:不要在这里使用 Stack 类,它继承自已被弃用的 Vector 类。)
Deque<String> stack = new ArrayDeque<>();
// 逐行读取文件并将行推入栈
String line;
while ((line = bufferedReader.readLine()) != null) {
stack.push(line);
}
// 从栈中弹出值并打印它们
while (stack.peek() != null) {
System.out.println(stack.pop());
}
} catch (IOException e) {
e.printStackTrace();
}
}
声明式("现代")方法:
public static void main(String args[]) {
String fileName = "randomFile.txt";
// 从输入文件创建流
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
// 创建一个栈(注意:不要在这里使用 Stack 类,它继承自已被弃用的 Vector 类。)
Deque<String> stack = new ArrayDeque<>();
// 将流中的行推入栈
stream.forEach(stack::push);
// 打印栈中的行(注意:栈在此处不会被修改!)
stack.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
英文:
Iterative ("classical") approach of reading a file and pushing the contents of it into a stack:
public static void main(String args[]) {
String fileName = "randomFile.txt";
// create a bufferedReader
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
// create a stack (Note: do not use the Stack class here, it is inherited from Vector which is deprecated.)
Deque<String> stack = new ArrayDeque<>();
// read the file line by line and push the lines into the stack
String line;
while ((line = bufferedReader.readLine()) != null) {
stack.push(line);
}
// pop the values from the stack and print them
while (stack.peek() != null) {
System.out.println(stack.pop());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Declarative ("modern") approach:
public static void main(String args[]) {
String fileName = "randomFile.txt";
// create stream from the input file
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
// create a stack (Note: do not use the Stack class here, it is inherited from Vector which is deprecated.)
Deque<String> stack = new ArrayDeque<>();
// push the lines from the stream into the stack
stream.forEach(stack::push);
// print the lines from the stack (Note: the stack is not modified here!)
stack.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论