将文本文件读入堆栈,按相反顺序打印出元素。

huangapple 未分类评论48阅读模式
英文:

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&lt;String&gt; stack){
        for(int i = 0; i&lt; stack.length(); i++){
            stack.push(i);
        }
    }

    static void stack_pop(Stack&lt;String&gt; stack){
        System.out.println(&quot;Pop :&quot;);

        for(int i = 0; i &lt; stack.length(); i++){
            String y = (String) stack.pop();
            System.out.println(y);
        }
    }

    public static void main(String args[]){
        BufferedReader br = new BufferedReader(new FileReader(&quot;randomFile.txt&quot;));
        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 = &quot;randomFile.txt&quot;;

   // 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&lt;String&gt; stack = new ArrayDeque&lt;&gt;();
        
        // 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 = &quot;randomFile.txt&quot;;
    // create stream from the input file
    try (Stream&lt;String&gt; 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&lt;String&gt; stack = new ArrayDeque&lt;&gt;();

        // 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();
    }
}

huangapple
  • 本文由 发表于 2020年5月4日 05:20:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61581775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定