0

My first language is PHP :) and newbie to Android and Java. I am trying to understand Array system in Java as it is quite different than PHP. So far understood Java has two different types of arrays (correct me if I am wrong). arr[] and ArrayList<> Also unlike PHP in Java we have to define datatypefor the array.

I am making a simple quiz app (without database) just to learn. For that I want to create a multidimensional array containing question, answer and options like this.

{
    {
        question,
        {option1, option2, option 3, ...}
        answer
    }

    {
        question,
        {option1, option2, option 3, ...}
        answer
    }

    {
        question,
        {option1, option2, option 3, ...}
        answer
    }

    ...
}

I know basics of creating a multidimensional array using arr[][] = {} but I don't know how I can add another array for options to get the similar result as above.

Update

So I want to populate list in this way.

enter image description here

7
  • You can make you own Model class, which will have question, options list and answer, then make arraylist of that model class Commented Oct 20, 2018 at 6:50
  • @AbdulKawee thanks for the suggestion.As I said, I am newbie so example code would be highly appreciated and helpful to understand. Commented Oct 20, 2018 at 6:51
  • Do you want structure like this i.e question list i.e q1,q2,q3,q4 answer list i.e a1,a2,a3,a4 you want both list in one list from which you get both ? Commented Oct 20, 2018 at 6:53
  • An ArrayList is not an array. ArrayList is a class. Don't use arrays in general. And don't use multi-dimensional arrays to store various things: define a class Question (as you would in PHP, BTW), and use a List<Question>. Any Java introductory book, or the Java tutorial, describes what a class is. It's a fundamental concept. You need to learn the language and do some research. Commented Oct 20, 2018 at 6:55
  • @SyedHamzaHassan In Java I am not so much sure which approach is better but I want to loop through Array to populate questions to create form with radio buttons for the question. Than when user submit I will compare with the answer. Commented Oct 20, 2018 at 6:55

2 Answers 2

2

You can do this by making your own model class as

public class MyCustomModel {

String question;
List<String> options;
String answer;

public MyCustomModel(String question, List<String> options, String answer){
 this.question = question;
 this.options = options;
 this.answer = answer;
}

 // getter and setter methods here

}

And then simply make an array list

private ArrayList<MyCustomModel> myData;

And then add data to List

myData.add(new MyCustomModel("question 1",myListOfOptions, "answer"));

Hope this helps :)

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

4 Comments

Thanks a lot. please have a look the reference image I have added on question.
Yes i have checked it, and this code can solve this problem,
Okay. one more question. Should I set myData in activity where I want to use array? Sorry if silly one but I am new and learning it.
please check the edited answer, and accept if it helped you
1

//You can do it like this, add model class

  public class Quizmodel {

String mAnswer = new String();
String mQuestion = new String();
List<String> mOptions = new ArrayList<>();
public String getmQuestion() {
        return mQuestion;
    }

    public void setmQuestion(String mQuestion) {
        this.mQuestion = mQuestion;
    }

public String getmAnswer() {
        return mAnswer;
    }

    public void setmAnswer(String mAnswer) {
        this.mAnswer= mAnswer;
    }





    public List<String> getmOptions() {
        return mOptions;
    }

    public void setmOptions(List<String> mOptions) {
        this.mOptions = mOptions;
    }
    }

//Set Values for model class, in main class;

   List<Quizmodel> mQuizList = new ArrayList<>();
    String mQuestion = new String();
    mQuestion = "Ask Question Here ?";
    List<String> mOptionsList = new ArrayList<>();
    mOptionsList.add("A1");
    mOptionsList.add("A2");
    mOptionsList.add("A3");

    Quizmodel mModel = new Quizmodel();
    mModel.setmQuestion(mQuestion);
    mModel.setmOptions(mOptionsList);
     mModel.setmAnswer("A2");

    mQuizList.add(mModel);

    //String m = "";
    for (int ind = 0; ind < mQuizList.size(); ind++) {
        System.out.println(mQuizList.get(ind).getmQuestion());
        //  Log.v("response",mQuizList.get(ind).getmQuestion());
        for (int index = 0; index < mQuizList.get(ind).getmAnswers().size(); index++) {
            System.out.print(mQuizList.get(ind).mAnswers.get(index) + " ");
           // m = m + mQuizList.get(ind).mAnswers.get(index) + " ";
        }
System.out.println(mQuizList.get(ind).getmAnswer());
    }

//Just follow step you will get desired result. //Added sample Input and Output.

Sample Output

Ask Question Here ?

Answer Options

A1 A2 A3

A2

//I hope it will bring ease.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.