简体   繁体   English

如何参考特定的阵列清单

[英]How do I refer to specific array list

I have an Arraylist with two arrays in it. 我有一个包含两个数组的Arraylist。 Depending on something from a different class, I want to either use the information from Array 1 or 2. However, I can't figure out how to refer indirectly to the specific array in the getQuestions method. 根据不同类的内容,我想使用数组1或2的信息。但是,我无法弄清楚如何在getQuestions方法中间接引用特定的数组。 I am able to receive the correct variable mQuestions1 or mQuestions2 in this class, in String format, and call this mVersion . 我可以在此类中以String格式接收正确的变量mQuestions1mQuestions2 ,并将其称为mVersion

My code: 我的代码:

public class QuestionLab {

ArrayList<Question> mQuestions1, mQuestions2;
private static QuestionLab sQuestionLab;
private Context mAppContext;

private QuestionLab(Context appContext) {
    mAppContext = appContext;

    mQuestions1 = new ArrayList<Question>();
    mQuestions1.add(new Question(1, R.drawable.a, state));
    mQuestions1.add(new Question(2, R.drawable.b, state));
    mQuestions1.add(new Question(3, R.drawable.c, state));

    mQuestions2 = new ArrayList<Question>();
    mQuestions2.add(new Question(1, R.drawable.a, state));
    mQuestions2.add(new Question(2, R.drawable.b, state));
    mQuestions2.add(new Question(3, R.drawable.c, state));
}

public static QuestionLab get(Context c) {
  if (sQuestionLab == null) {sQuestionLab = new QuestionLab(c.getApplicationContext());}
  return sQuestionLab;
}

public Question getQuestion(int nr) {
 for (Question c : mQuestions) {if (c.getNr() == (nr)) return c;} return null;
}

public ArrayList<Question> getQuestions(){return mQuestions1}

Writing mQuestions1 does work, but doesn't allow me to change between 1 & 2, so I want something like Arraylist(mVersion) 编写mQuestions1确实可以,但是不允许我在1和2之间进行更改,因此我需要Arraylist(mVersion)类的东西

Thanks in advance! 提前致谢!

it's rather easy. 这很容易。

Build a HashMap or another Basic Class which hold several childs. 构建一个HashMap或另一个拥有几个孩子的基本类。

Do you want to randomize it which question should be choosen? 您是否要随机选择哪个问题? Why dont you create a helperClass which is called (for example Question) 为什么不创建一个被称为的helperClass(例如Question)

which keep an array(list)? 哪个保留一个数组(列表)?

class Question {
  Arraylist<String> questions;
  //;getter and setter
}

public ArrayList<Question> getQuestions(){
 for (Question c : mQuestions) {
      ArrayList<String> dummyArray = c.getQuestions();
       if (c.size() > 0) {
       //randomize it, get it out, whateer.
       } 
 }
}

But getting back to your Question (and a Solution which is not a good technique doing this). 但是请回到您的问题(以及解决方案,这不是一个很好的方法)。

public ArrayList>Questions> getQuestions() { 
    if (question_array_1 != null && question_array_2 != null) { 
       int qCnt = (questions_array_1.size() > question_array_2.size()) ? question_array_1.size() : question_array_2.size();
     for (int i = 0; <= qCnt; i++) { 
       String myQuestion = (Math.random(0,1) == 1) ? question_array_2.getQuestion() : question_array_1.getQuestion();
   //or merge it to another arraylist... 
     }
  } 
}

Another Technique would be storing it directly into a SQLITEDatabase (which i pfefer!). 另一种技术是将其直接存储到SQLITEDatabase中(我很喜欢!)。

A last but not not least (and maybe not a good technique in your case) is adding all into one with 最后但并非最不重要的一点(在您的情况下可能不是一种很好的技术)是将所有内容与

ArrayList<Question> array_list_all =  new ArrayList<Question>();
array_list_all.addAll(first_question_array);
array_list_all.addAll(second_question_array);

Then you can foreach it and get the Question which triggers your level or questionID. 然后,您可以foreach它并获取触发您的级别或questionID的问题。

...
public static final int USE_FIRST = 0;
public static final int USE_SECOND = 1;


...
public Question getQuestion(final int mode, final int nr) {
    ArrayList<Question> listToUse = null;
    if (mode == USE_FIRST) {
         listToUse = mQuestions1;
    } else if (mode == USE_SECOND) {
         listToUse = mQuestions2;
    }
    for (Question q : listToUse) ...
}

If you have more than two you can use Map to store lists of questions: 如果您有两个以上,则可以使用Map存储问题列表:

Map<Integer, ArrayList<Question>> questionMap = new HashMap<Integer, ArrayList<Question>>();
questionMap.put(1, mQuestions1);
questionMap.put(2, mQuestions2);
questionMap.put(3, mQuestions3);

1 -> ArrayList #1 1-> ArrayList#1

2 -> ArrayList #2 2-> ArrayList#2

3 -> ArrayList #3 3-> ArrayList#3

Hope I got your question right 希望我能正确回答你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM