How to access Java array list data from another class

后端 未结 2 682
执笔经年
执笔经年 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: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 questions;
    
    public ArrayList 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.

提交回复
热议问题