How to access Java array list data from another class

后端 未结 2 649
执笔经年
执笔经年 2021-01-29 15:50

I\'m trying to make a quiz in Java but I\'m having trouble accessing the array list data from the tester class and therefore my question text isn\'t showing up. I have three cla

相关标签:
2条回答
  • 2021-01-29 16:15

    After some alterations, I was able to get your code running. But I have to warn you, there are quite some changes:

    • QuizTester now only has a main method to start the program. It will initialize and fill the list with questions and then pass it to the QuizSetUp instance
    • I didn't have your Question class, so I reduced it to an ArrayList<String> (just to make sure, that the questions could be passed)
    • And I didn't hvae your QuizInterface class so I helped myself with a small implementation that would simply print out the question when a new question gets set

    QuizInterface (small helper class)

    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 (heavily reduced)

    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
    }
    

    QuizTester (only needs the main method)

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

    Those three classes can compile as they are and when I ran the program I got this output

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

    I know this is a lot different from what you have, the only big change I did was passing the newly create questions list directly to the QuizSetUp instance – so no accessing any static lists.

    0 讨论(0)
  • 2021-01-29 16:18

    Looking at this line:

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

    where is the getQuestionList() implemented? It looks like a method call, except that QuizSetUp doesn't declare a getQuestionList() method. It is in a different class.

    Conclusion: the code that you've shown us in the question won't even compile.


    I should point that this (in QuezSetup) is very bad style, dnd liable to cause confusion.

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

    While this.questions looks like it is referring to an instance variable, it is actually referring to a static variable. The this is misleading.

    0 讨论(0)
提交回复
热议问题