如何从另一个类访问Java数组列表数据

我正在尝试用Java进行测验,但是我无法从测试器类访问数组列表数据,因此我的问题文本没有显示出来。 我有三节课; 测试器,测验界面和测验设置。 我已经玩了一段时间了,我很确定我开始让事情变得更糟,所以我想我会在这里发帖。

这些问题被添加到Tester文件的数组列表中,但我似乎无法在此方法的设置类中访问它:

public void setQuestion(int randIndex) { qi.getQuText().setText(getQuestionList().get(randIndex).getQuestionText()); } 

预期输出是从数组列表中随机提出问题并显示问题文本,但没有出现任何内容,它是空白的。

我是Java和编程的新手,所以欢迎任何详细的答案! 提前致谢。

 import java.util.ArrayList; public class QuizTester { private static ArrayList questions; //declares arrayList to holds the questions public static void main(String[] args) { QuizSetUp theQuiz = new QuizSetUp(); questions = new ArrayList(); //constructor questions.add(new FillInBlank("____________ is the ability of an object to take many forms.", "Polymorphism")); questions.add(new FillInBlank("The process where one object acquires the properties of another is called __________", "inheritance")); questions.add(new FillInBlank("The ___________ keyword is used by classes to inherit from interfaces", "implements")); questions.add(new MultipleChoice("Which programming technique can be used to prevent code and data from being randomly accessed by other code defined outside the class?", "Polymorphism", "Encapsulation", "Inheritance", "Construction", "Encapsulation")); theQuiz.pickQuestion(); } public ArrayList getQuestionList() { return this.questions; } } 

////////////////////////测验设置档案。

 public class QuizSetUp { private QuizInterface qi; private QuizTester test; //private ArrayList questions; //declares arrayList to holds the questions private int counter = 1; Random random; int randIndex; public QuizSetUp() { setInterface(); //questions = new ArrayList(); //constructor } private enum QuAnswer { CORRECT,INCORRECT } public void setInterface() { qi = new QuizInterface(); test = new QuizTester(); //add action listeners to each of the buttons ActionListener cl = new ClickListener(); qi.getNextBtn().addActionListener(cl); qi.getStartQuizBtn().addActionListener(cl); //allows users to press enter to start quiz rather than having to click quiz button KeyListener ent = new KeyBoardListener(); qi.getUName().addKeyListener(ent); qi.getUPassword().addKeyListener(ent); } public void pickQuestion() { randQuestion(); setQuestion(randIndex); //setAnswer("A", randIndex); //setAnswer("B", randIndex); //setAnswer("C", randIndex); //setAnswer("D", randIndex); //setCorrectAnswer(randIndex); //qi.resetTimer(); } public void setQuestion(int randIndex) { qi.getQuText().setText(getQuestionList().get(randIndex).getQuestionText()); } public void setNextQuestion() { //qi.getTimer().cancel(); //qi.cancelInterval(); if (counter < 5) { //users must answer five questions to complete quiz pickQuestion(); } else { //JOptionPane.showMessageDialog(qi.getPanels(), "End of quiz"); //switch to end panel to show results of quiz } } public int randQuestion() { random = new Random(); randIndex = random.nextInt(questions.size()); return randIndex; } //inner listener class for buttons private class ClickListener implements ActionListener { public void actionPerformed(ActionEvent evt) { if (evt.getSource() == qi.getStartQuizBtn()) { qi.setEnteredName(qi.getUName().getText()); qi.setEnteredPass(qi.getUPassword().getPassword()); validateInput(); } else if (evt.getSource() == qi.getNextBtn()) { counter++; if (counter == 5) { qi.getNextBtn().setText("Finish Quiz"); //changes next button text on final question } if (counter  0) { //presence check on password if (qi.getEnteredPass().length > 0) { //ensures password is at least 6 char long if(qi.getEnteredPass().length > 5) { qi.getCards().next(qi.getPanels()); //getPanels() == cardPanel } else { JOptionPane.showMessageDialog(null, "Your password must be at least six characters long.", "Password Violation", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(null, "Your did not enter a password.", "Password Violation", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(null, "You did not enter a username. Please try again.", "Username Violation", JOptionPane.WARNING_MESSAGE); } } 

}

经过一些改动后,我能够让你的代码运行起来。 但我必须警告你,有一些变化:

  • QuizTester现在只有一个启动程序的main方法。 它将初始化并用问题填充列表,然后将其传递给QuizSetUp实例
  • 我没有你的Question类,所以我将它缩减为ArrayList (只是为了确保可以传递问题)
  • 而且我没有你的QuizInterface类,所以我帮助自己一个小的实现,只需在设置新问题时打印QuizInterface

QuizInterface (小助手类)

 public class QuizInterface { private String text; public QuizInterface() { } public String getText() { return text; } public void setText(String text) { this.text = text; System.out.println("question text = "+this.text); // this is just to make sure it worked } } 

QuizSetUp (大幅减少)

 public class QuizSetUp { private QuizInterface qi; private ArrayList questions; // uncommented, it's needed now private int counter = 1; Random random; int randIndex; // I chose to pass the list with the constructor but the setQuestions() will do as well public QuizSetUp(ArrayList questions) { this.questions = questions; setInterface(); } // NEW method – but it's not needed public ArrayList getQuestions() { return questions; } // NEW method – but it's not needed public void setQuestions(ArrayList questions) { this.questions = questions; } private enum QuAnswer { CORRECT, INCORRECT } public void setInterface() { qi = new QuizInterface(); // test = new QuizTester(); // this is no longer needed since QuizTester is only used to start the program } public void pickQuestion() { randQuestion(); setQuestion(); // randIndex is already a global variable in this class, no need to pass with the method call } public void setQuestion() { // QuizInterface has a new method now called "setText()" // so here we access the list "questions" (it is already initialized, because we pass it to this class when constructing it) // this.randIndex is global, so we can use it directly in this method as an index to the questions list (as you already did it) qi.setText(this.questions.get(this.randIndex)); } public void setNextQuestion() { //qi.getTimer().cancel(); //qi.cancelInterval(); if (counter < 5) { //users must answer five questions to complete quiz pickQuestion(); } else { //JOptionPane.showMessageDialog(qi.getPanels(), "End of quiz"); //switch to end panel to show results of quiz } } public int randQuestion() { random = new Random(); randIndex = random.nextInt(questions.size()); return randIndex; } // .... the rest I left out here because it is not needed for this little test } 

QuizTester (只需要main方法)

 public class QuizTester { public static void main(String[] args) { ArrayList questions = new ArrayList<>(); //as you can see I replaced the List with a list of Strings (because I didn't have your Question class) // so these are only strings... questions.add("____________ is the ability of an object to take many forms."); questions.add("The process where one object acquires the properties of another is called __________"); questions.add("The ___________ keyword is used by classes to inherit from interfaces"); questions.add("Which programming technique can be used to prevent code and data from being randomly accessed by other code defined outside the class?"); // here I create the QuizSetUp instance and pass the list right with the constructor QuizSetUp theQuiz = new QuizSetUp(questions); // if everything works out, calling this method // should pick a new question, set it to the QuizInterface // and the QuizInterface (the helper version I made) will print it out theQuiz.pickQuestion(); } } 

这三个类可以按原样编译,当我运行程序时,我得到了这个输出

 question text = The ___________ keyword is used by classes to inherit from interfaces 

我知道这与你所拥有的有很大不同,我所做的唯一重大改变就是将新创建的问题列表直接传递给QuizSetUp实例 - 因此不能访问任何静态列表。

看看这一行:

 qi.getQuText().setText(getQuestionList().get(randIndex).getQuestionText()); 

getQuestionList()在哪里实现? 它看起来像一个方法调用,除了QuizSetUp没有声明getQuestionList()方法。 这是一个不同的阶级。

结论:您在问题中向我们展示的代码甚至无法编译。


我应该指出这个(在QuezSetup )是非常糟糕的风格,并且容易引起混淆。

 private static ArrayList questions; public ArrayList getQuestionList() { return this.questions; } 

虽然this.questions 看起来像是指一个实例变量,但它实际上是指一个静态变量。 this是误导。