Java 根据其ID存储和获取数据

Java 根据其ID存储和获取数据,java,arraylist,store,Java,Arraylist,Store,我正在尝试存储数据并根据它们的ID将其取回 int Q_ID -------> Id of the Question int Type_Question ----> Which type is the Question ArrayList<String> Options ----> The options are multiple and are determined at run based on users selection of the op

我正在尝试存储数据并根据它们的ID将其取回

int Q_ID -------> Id of the Question
int Type_Question ----> Which type is the Question
ArrayList<String> Options ----> The options are multiple and are determined at run         based on users selection of the options
是否有一种方法可以存储它们,以便在输入Q_ID=0并键入_Question=1时;它只返回手动泵和油井,如果输入Q_ID=1并键入_question=1,则只返回random和answer。
这可能太明显了,但我想不出任何东西。有什么想法吗?

一种简单的、有点粗糙的方法可以做到这一点,而无需定义自己的类型,就是将它们存储在HashMap中,使用一个将Q_ID和Type_问题组合为单个串联字符串的字符串,如下所示:

HashMap<String, ArrayList<String>> yourHashMap = new HashMap<String, ArrayList<String>>();

ArrayList<String> handPumpWell = new ArrayList<String>();
handPumpWell.add("handpump");
handPumpWell.add("well");
yourHashMap.put("0,1", handPumpWell);

ArrayList<String> randomAnswer = new ArrayList<String>();
randomAnswer.add("random");
randomAnswer.add("answer");
yourHashMap.put("1,1", randomAnswer);
对于
Q\u ID=0
Type\u Question=1
将返回一个
数组列表
,其中包含
“手动泵”
“井”


但是,您必须确保Q_ID始终位于Type_问题之前,因为重新排列它们不会返回正确的结果。

表达式的类型必须是数组类型,但它解析为HashMap,这会导致此错误。.有什么想法吗?@zeeshandar现在尝试编译它-我有一些错误。yoursHashMap.put(“1,1”,randomAnswer); 而不是你的HashMap[“1,1”]=randomAnswer;感谢它现在的工作..干杯不同的数据结构适用于不同的事情。为了高效查找,您需要一个“字典”数据结构。Java中的一个很好的候选者是HashMap,例如“HashMap”。非常感谢您的建议,我已经解决了它:)
HashMap<String, ArrayList<String>> yourHashMap = new HashMap<String, ArrayList<String>>();

ArrayList<String> handPumpWell = new ArrayList<String>();
handPumpWell.add("handpump");
handPumpWell.add("well");
yourHashMap.put("0,1", handPumpWell);

ArrayList<String> randomAnswer = new ArrayList<String>();
randomAnswer.add("random");
randomAnswer.add("answer");
yourHashMap.put("1,1", randomAnswer);
yourHashMap[Q_ID + "," + Type_Question]