0

I created an array in android studio. What does the code below mean?

private question[] questionbank = new question[]{
    new question(R.string.question_africa, true),
    new question(R.string.question_oceans, false),
    new question(R.string.question_mideast, true),
    new question(R.string.question_america, false),
    new question(R.string.question_asia, true);

I initialized an array from a class called question shown below

public class question {
    private int TextInput;
    private boolean AnswerInput;
    private boolean hasCheated;


    public boolean isHasCheated() {
        return hasCheated;
    }

    public void setHasCheated(boolean hasCheated) {
        this.hasCheated = hasCheated;
    }

    public question(int holdsTextInput, boolean holdsAnswerInput) {
        this. TextInput = holdsTextInput;
        this.AnswerInput = holdsAnswerInput;
    }

    public int getTextInput() {
        return TextInput;
    }

    public boolean getAnswerInput() {
        return AnswerInput;
    }
    public void setTextInput(int holdsTextInput) {
        this.TextInput = holdsTextInput;
    }
    public void setsAnswerInput(boolean holdsAnswer) {
        this.AnswerInput = holdsAnswer;
    }
}

However, I wanted some clarification, for instance, what exactly was done here? Did I reference the class question and create an array called questionbank? Also if so why do I need to put in each value as a new question?

2
  • 3
    By the way class name should be in upper camel case Commented Jan 11, 2016 at 7:09
  • Search for the Google Java style guide; you're not following code conventions, and it makes the code much more difficult to read. Commented Jan 11, 2016 at 7:28

5 Answers 5

3

Did I reference the class question and create an array called questionbank?

You created an array that can only store objects of type question. The array is itself an object, and a reference to the array was assigned to the field named questionbank.

Note that the array doesn't have a name, it's just referenced from a field with that name. You can assigning the same reference to fields and variables of other names, and it would still be the same array.

Also if so why do I need to put in each value as a new question?

In Java, an array of objects is actually an array of object references, so when you create an array, you don't actually create any objects.

That is why you have to create 5 instances of question and assign the references to them to the array.

If you didn't do that, you'd have an array of null values.

Sign up to request clarification or add additional context in comments.

Comments

1

First of all we will create a single object named questionbank which type is Array and which holds elements of question type.

Step-1: By declaring a reference variable as bellow:

 private question[] questionbank.....;

questionbank holds the reference of a single object which type is Array. By this part no objects are created.


Step-2: By writing bellow part we are creating a new single object of Array type which element type is question

  new question[]{......};


Step-3: And finally we are initializing our newly created single array object by putting some question objects on it:

{
new question(R.string.question_africa, true),
new question(R.string.question_oceans, false),
new question(R.string.question_mideast, true),
new question(R.string.question_america, false),
new question(R.string.question_asia, true)..}

Here new question(..) using each time because before putting a question object in questionbank array object we need to create them using new keyword.

Comments

0

Did I reference the class question and create an array called questionbank?

question[] questionbank = new question[];

This means create an array named questionbank that will contain only question objects. The name is used to refer to the array later on:

question q = questionbank[0];  // get the first element from the array

An alternate example:

String[] arr = new String[5];

Here you created an array named arr that will contain only 5 Strings;

if so why do I need to put in each value as a new question?

The questionbank array can only hold question objects. Therefore adding objects using new question(...) is the correct approach.

Comments

0

Yes, you created an array called questionbank.

I think you're confused about the notion of a class. The "new" keyword is just a fancy way to call the question class's constructor:

public question(int holdsTextInput, boolean holdsAnswerInput)

Each time you call this constructor, you create a new instance of question. For example, the line of code:

new question(R.string.question_africa, true)

Creates an instance of the question class, with its TextInput field set to R.string.question_africa and its AnswerInput field set to true.

On the other hand, the line:

new question(R.string.question_oceans, false)

Creates an instance of the question class with TextInput set to R.string.question_oceans and AnswerInput set to false.

Because the "new" keyword was used both times, these two instances of question have no relation to each other; changing one won't change the other.

You don't necessarily have to call new every time. For example, you could do:

question q = new question(R.string.question_africa, true);
private question[] questionbank = new question[]{q, q, q, q, q};

But as you can see, each questions stored in the array really references the same object (q) so they'll have the same fields (TextInput, AnswerInput) and changing one element of the array would change all other elements of the array (because they all point to the same instance of question.

Comments

0

Firstly try using proper casing for Class Names.

Question[] question = new Question[size];

Then you use Constructors or methods based on your requirement.

Constructor Access:

 question[0] = new Question(x,y,z);

Method access:

question[0] = new Question();
.
.
question[size] = new Question();

question[0].isHasCheated();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.