从ArrayList检索数据?

时间:2016-09-22 19:15:09

标签: java android list arraylist

我有一个Object类型的ArrayList,该数组列表包含另一个arraylist obj。

实施例 -

ArrayList<Object> obj=new ArrayList<Object>();
ArrayList<Object> obj2=new ArrayList<Object>();

obj2.add(101);//contain core data



obj.add(obj2);//storing obj2 into the obj

我如何从obj2获得101?

3 个答案:

答案 0 :(得分:0)

我不知道你为什么会这样做,但首先你会得到它存储的索引,然后从ArrayList中获取元素。我建议使用List接口,而不是使用ArrayList接口的实现。

obj2.get(obj2.indexOf(101))

使用列表界面并输入推理

List<Object> obj=new ArrayList<>();
List<Object> obj2=new ArrayList<>();

答案 1 :(得分:0)

我同意@Jonny Henley的简单ArrayList documentation查询应该就够了,但是,如果海​​报是新手,这是我的答案。

// Your code
ArrayList<Object> obj=new ArrayList<Object>();
ArrayList<Object> obj2=new ArrayList<Object>();

obj2.add(101);//contain core data
obj.add(obj2);//storing obj2 into the obj

// My code: Get 101 from obj2
// Because obj2 is in-scope here, and assuming that you know
// obj2 is not empty...

Object data = obj2.get(0);

// Above, data will be an Integer object due to auto-boxing
// If it is null, auto-unboxing will throw NullPointerException
// If data == null, set dataInt to any value to indicate that.
// Here, I am setting it to -1, assuming -1 is an invalid value
// for your application.

int dataInt = (data != null) ? data : -1;

答案 2 :(得分:0)

通过使用这个approche,我能够从obj获取数据;

Object getData=obj.get(0);
ArrayList<Object> arrayListObject=new ArrayList<Object>();
arrayListObject=(ArrayList)getData;
int a=(int)arrayListObject.get(0);