代码之家  ›  专栏  ›  技术社区  ›  Code Lover

Android创建多维数组并获取值

  •  0
  • Code Lover  · 技术社区  · 7 年前

    (如果我错了,请纠正我) . arr[] ArrayList<> datatype 对于数组。

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

    arr[][] = {} 但我不知道如何为options添加另一个数组以获得与上面类似的结果。

    更新

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Syed Hamza Hassan    7 年前

    //你可以这样做,添加模型类

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

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

    //只要遵循步骤,你就会得到想要的结果。 //增加了样本输入和输出。

    样本输出

    答案选项

    A1 A2 A3

    A2级

    //我希望它能带来轻松。

        2
  •  2
  •   Abdul Kawee    7 年前

    您可以通过将自己的模型类作为

    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
    
    }
    

    然后简单地做一个 array list

    private ArrayList<MyCustomModel> myData;
    

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