2016-09-22 58 views
-7

我有一個ArrayList的對象類型,該數組列表包含另一個ArrayList obj。檢索數據friom ArrayList?

EX-

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?

+0

請參閱'ArrayList's'文檔,這是一個簡短的谷歌搜索了。 –

回答

0

我不知道你爲什麼要這樣做,但首先你會得到它存儲的索引,然後從ArrayList中獲取元素。我建議使用List接口,而不是ArrayList的接口的實現。

obj2.get(obj2.indexOf(101)) 

使用List接口和類型推斷

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

我@Jonny亨利同意,一個簡單的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; 
0

通過使用此Approche我可以從obj獲取數據;

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