将文本文件中的数据保存到数组列表中时出现问题

Issues saving data from a text file into an array list

所有阅读本文的人您好,我现在花了几个小时试图找出我在编写的 JavaFX 琐事程序中遇到的数组列表问题的根源。我终于缩小了问题的范围,我只是无法弄清楚是什么原因造成的。基本上,我有一个 while 循环,它会一直读取代码行,直到它到达末尾,并且可以正常工作。在每个循环结束时,我将读取的数据以对象的形式添加到 ArrayList 中,该对象保存读取的数据。它有效,因为当我在数组列表中的每个索引处打印对象的参数之一时,它有效。然而,当我走出 while 循环时,ArrayList 的每个索引都包含完全相同的数据,这始终是我保存的最后一个问题。

代码如下:

        while ((line = questionStream.readLine()) != null) {

            // The question
            String inquiry = line;

            // The first possible response
            line = questionStream.readLine();
            String[] responses = new String[4];
            responses[0] = line;

            // The second possible response
            line = questionStream.readLine();
            responses[1] = line;

            // The third possible response
            line = questionStream.readLine();
            responses[2] = line;

            // The fourth possible response
            line = questionStream.readLine();
            responses[3] = line;

            // The fact to display once the question has been answered
            line = questionStream.readLine();
            String fact = line;

            // Space in between questions
            questionStream.readLine();

            // Adding the question
            questions.add(new Question(inquiry, responses, fact));
            Console.print(questions.get(temp).getInquiry());
            temp++;
        }

        questionStream.close();
        
        for (int i = 0; i < questions.size(); i++) {
        Console.print(questions.get(i).getInquiry());
        }

输出如下:

How many countries border France?
What sea creature has 3 hearts?
The Simpsons is the longest running tv series. What is the name of the janitor?
What was the product of the first ever TV advertisement?
What TV series features a reference to or a picture of Superman in almost every episode?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?
How many castaways were on Gilligans Island?

所以我真的很困惑 ArrayList 如何完美地保存所有正确的数据,直到它退出 while 循环,然后它只保存文本文件中的最终问题。

感谢您的帮助!

问题是您没有在 Question class 中使用实例变量,而是 class 变量。这意味着您的 Question class 应该看起来像这样才能按预期工作:

package trivia.questions;
 
public class Question {
 
    // Data fields, don't use static!
    private String inquiry;
    private String[] answers;
    private String fact;
    private String correctAnswer;
 
... 
    /**
     * Overloaded constructor
     * 
     * @param inquiry the question
     * @param answers the answers
     * @param fact    the cool fact for the correct answer
     */
 
    public Question(String inquiry, String[] answers, String fact) {
        this.inquiry = inquiry;
        this.answers = answers;
        this.fact = fact;
     }

对变量使用 static 将使该变量在 Question 变量的所有实例之间共享。

在每次迭代中,您打印出当前状态,这似乎没问题,但在您完成后,您多次打印出相同状态,该状态在 class Question 的不同副本之间共享。

这是Java中面向对象编程的一个主要概念。