dubbz
dubbz

Reputation: 1

How does Java know that a class can be an array?

I am learning Android development with the Big Nerd Ranch Guide where you need to create a quiz application, and am confused about how a class can be made into an array which is not specified as one.

I have created the Question class below which stores the question and answer:

public class Question {

    private int mTextResId;
    private boolean mAnswerTrue;

    public Question(int TextResId, boolean AnswerTrue) {
        mTextResId = TextResId;
        mAnswerTrue = AnswerTrue;

//Setters and Getter below

Then in the main activity have added:

private Question[] mQuestionArray = new Question[]{
            new Question(R.string.question_animals,true),
            new Question(R.string.question_australia, true),
};

Can any class be made into an array using the [] after the class name?

Why is it that the Question class can be used as an array, and be used to collect the question with answer, even though this is not specified in the class?

Thank you for your help!

Upvotes: 0

Views: 36

Answers (1)

Andy Turner
Andy Turner

Reputation: 140484

Can any class be made into an array using the [] after the class name?

Yes.

And you can even make an array of arrays of arrays of arrays of any class, should you so desire:

Question[][][][]

And you can do the same with any primitive type too:

int[] and int[][][][]

Upvotes: 3

Related Questions