我是android的新手,我需要一些帮助才能在数组中创建一个混合数组。
这就是我需要的:
myArray = {["String", int, int, int], ["String", int, int, int], ["String", int, int, int]};
我怎样才能创造出类似的东西?以及如何在以后访问这些变量?
非常感谢你的帮助。
答案 0 :(得分:2)
您创建一个对象数组
该对象包含String
,int
,int
,int
ArrayList<MyObject> mArrayList = new ArrayList<MyObject>();
mArrayList.add(new MyObject("String1", 1, 2, 3));
答案 1 :(得分:1)
myArray = {["String", int, int, int], ["String", int, int, int], ["String", int, int, int]};
=&GT; 创建一个类来存储数据
public class MyClass
{
private String string;
private int int1;
private int int2;
private int int3;
public MyClass()
{
}
public MyClass(String string, int int1, int int2, int int3)
{
this.string = string;
this.int1 = int1;
this.int2 = int2;
this.int3 = int3;
}
public String getString()
{
return string;
}
public MyClass setString(String string)
{
this.string = string;
return this;
}
public int getInt1()
{
return int1;
}
...
}
=&GT;使用集合
List<MyClass> list = new ArrayList<MyClass>();
list.add(new MyClass("Hello", 1, 2, 3);
list.add(new MyClass("World", 4, 5, 6);
MyClass mc = list.get(0);
for(MyClass myClass : list)
{
android.util.Log.i(getClass().getSimpleName(), "Content: " + myClass.getString() + " " + myClass.getInt1() + " " + myClass.getInt2() + " " + myClass.getInt3());
}