我的第一语言是 PHP :),并且是 Android 和 Java 的新手。我试图理解 Java 中的数组系统,因为它与 PHP 有很大不同。到目前为止,Java 有两种不同类型的数组(如果我错了,请纠正我)。 arr[]
和ArrayList<>
与 Java 中的 PHP 不同,我们必须定义 datatype
对于数组。
我正在制作一个简单的测验应用程序(没有数据库)只是为了学习。为此,我想创建一个包含问题、答案和选项的多维数组,如下所示。
{
{
question,
{option1, option2, option 3, ...}
answer
}
{
question,
{option1, option2, option 3, ...}
answer
}
{
question,
{option1, option2, option 3, ...}
answer
}
...
}
我了解使用 arr[][] = {}
创建多维数组的基础知识但我不知道如何添加另一个数组来获得与上面类似的结果。
更新
所以我想以这种方式填充列表。
最佳答案
您可以通过将自己的模型类制作为来实现此目的
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
}
然后简单地创建一个数组列表
private ArrayList<MyCustomModel> myData;
然后将数据添加到List
myData.add(new MyCustomModel("question 1",myListOfOptions, "answer"));
希望这有帮助:)
关于java - Android创建多维数组并获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52903157/