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

如何从另一个类访问Java数组列表数据,java,arrays,object,arraylist,Java,Arrays,Object,Arraylist,我试图用Java做一个测验,但是我在从tester类访问数组列表数据时遇到了问题,因此我的问题文本没有显示出来。我有三节课;测试仪、测验界面和测验设置。我已经玩了一段时间,我很确定我开始让事情变得更糟,所以我想我应该在这里发布 这些问题已添加到测试仪文件中的数组列表中,但我似乎无法在该方法的设置类中访问这些问题: public void setQuestion(int randIndex) { qi.getQuText().setText(getQuestionList().get(ra

我试图用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<Question> questions; //declares arrayList to holds the questions

    public static void main(String[] args) {
            QuizSetUp theQuiz = new QuizSetUp();
            questions = new ArrayList<Question>(); //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<Question> getQuestionList() {
            return this.questions;
    }


}
import java.util.ArrayList;
公开课{
私有静态ArrayList问题;//声明ArrayList以保存问题
公共静态void main(字符串[]args){
QuizSetUp测验=新建QuizSetUp();
questions=new ArrayList();//构造函数
添加(新的FillInBlank(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
添加(新的FillInBlank(“一个对象获取另一个对象的属性的过程称为继承”);
添加(新的FillInBlank(“类使用-uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
添加(新的MultipleChoice(“哪种编程技术可用于防止代码和数据被类外定义的其他代码随机访问?”),
“多态性”、“封装”、“继承”、“构造”、“封装”);
小测验。选择问题();
}
公共阵列列表getQuestionList(){
把这封信还给我。问题;
}
}
////////////////////////测验设置文件

public class QuizSetUp {
    private QuizInterface qi;
    private QuizTester test;
    //private ArrayList<Question> questions; //declares arrayList to holds the questions
    private int counter = 1;
    Random random;
    int randIndex;

    public QuizSetUp() {
            setInterface();
            //questions = new ArrayList<Question>(); //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 < 6) {
                                    qi.getQuProgress().setText(counter + " of 5");
                            } else {
                                    //shuffle to end panel
                            }
                    }
            }
    }

    //inner listener class for key presses
    private class KeyBoardListener implements KeyListener {
            public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                            qi.setEnteredName(qi.getUName().getText());
                            qi.setEnteredPass(qi.getUPassword().getPassword());
                            validateInput();
                    }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                    // TODO Auto-generated method stub

            }
            @Override
            public void keyTyped(KeyEvent e) {
                    // TODO Auto-generated method stub

            }
    }

    //method to validate input by user to log in
    public void validateInput() {
            //presence check on username
            if (qi.getEnteredName().length() > 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);
            }
    }
公共类QuizSetUp{
私家侦探;
私人测验;
//private ArrayList questions;//声明ArrayList以保存问题
专用整数计数器=1;
随机;
国际贸易指数;
公共QuizSetUp(){
setInterface();
//questions=new ArrayList();//构造函数
}
私有枚举量程{正确,不正确}
公共void setInterface(){
qi=新的QuizInterface();
测试=新的QuizTester();
//向每个按钮添加操作侦听器
ActionListener cl=新建ClickListener();
qi.getNextBtn().addActionListener(cl);
qi.getStartQuizBtn().addActionListener(cl);
//允许用户按enter键开始测验,而无需单击测验按钮
KeyListener ent=新的KeyBoardListener();
qi.getUName().addKeyListener(ent);
qi.getUPassword().addKeyListener(ent);
}
公共问题{
问题();
设定问题(随机指数);
//setAnswer(“A”,randIndex);
//setAnswer(“B”,randIndex);
//setAnswer(“C”,randIndex);
//setAnswer(“D”,随机指数);
//setCorrectAnswer(randIndex);
//qi.resetTimer();
}
公共无效设置问题(int randIndex){
qi.getQuText().setText(getQuestionList().get(randIndex.getQuestionText());
}
public void setNextQuestion(){
//qi.getTimer().cancel();
//qi.cancelInterval();
如果(计数器<5){//用户必须回答五个问题才能完成测试
pickQuestion();
}否则{
//showMessageDialog(qi.getPanels(),“测验结束”);
//切换到结束面板以显示测验结果
}
}
公共问题({
随机=新随机();
randIndex=random.nextInt(questions.size());
回归指数;
}
//按钮的内部侦听器类
私有类ClickListener实现ActionListener{
已执行的公共无效操作(操作事件evt){
if(evt.getSource()==qi.getStartQuizBtn()){
qi.setEnteredName(qi.getUName().getText());
qi.setEnteredPass(qi.getUPassword().getPassword());
验证输入();
}else if(evt.getSource()==qi.getNextBtn()){
计数器++;
如果(计数器==5){
qi.getNextBtn().setText(“完成测验”);//更改最后一个问题的下一步按钮文本
}
如果(计数器<6){
qi.getQuProgress().setText(计数器+“共5个”);
}否则{
//移动到端面板
}
}
}
}
//用于按键的内部侦听器类
私有类KeyBoardListener实现KeyListener{
按下公共无效键(按键事件e){
如果(例如getKeyCode()==KeyEvent.VK_ENTER){
qi.setEnteredName(qi.getUName().getText());
qi.setEnteredPass(qi.getUPassword().getPassword());
验证输入();
}
}
@凌驾
公共无效密钥已释放(密钥事件e){
//TODO自动生成的方法存根
}
@凌驾
public void keyTyped(KeyEvent e){
//TODO自动生成的方法存根
}
}
//方法验证用户登录时的输入
public void validateInput(){
//用户名的状态检查
如果(qi.getEnteredName().length()>0){
//密码状态检查
如果(齐
qi.getQuText().setText(getQuestionList().get(randIndex).getQuestionText());
private static ArrayList<Question> questions;

public ArrayList<Question> getQuestionList() {
        return this.questions;
}
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
    }
}
public class QuizSetUp {

    private QuizInterface qi;
    private ArrayList<String> 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<String> questions) {
        this.questions = questions;
        setInterface();
    }

    // NEW method – but it's not needed
    public ArrayList<String> getQuestions() {
        return questions;
    }

    // NEW method – but it's not needed
    public void setQuestions(ArrayList<String> 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
}
public class QuizTester {

    public static void main(String[] args) {
        ArrayList<String> 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