2013-05-02 71 views
-1

我有一個包含對象的ArrayList。我有兩個不同的數組列表,其中一個包含字符串和其他整數。現在我需要從父列表中獲取字符串和整數,並將其放入新的兩個數組列表中。列表列表如下。要從對象ArrayList中分離整數和字符串ArrayList

ArrayList<Object> lDeltaAttrList = new ArrayList<Object>(); 
ArrayList<String> lDeltaAttrListString = new ArrayList<String>(); 
ArrayList<Integer> lDeltaAttrListInteger = new ArrayList<Integer>(); 

請幫忙。

回答

4

您只需檢查Object是否是String或Integer並將其放入右側列表中。

for(Object o : lDeltaAttrList) { 
    if(o instanceof String) { 
     lDeltaAttrListString.add(o); 
    } else if(o instanceof Integer) { 
     lDeltaAttrListInteger.add(o); 
    } 
} 
+1

它的.add(),而不是.put() – 2013-05-02 12:19:39

+0

是的,我糾正它之前,您的評論:-P – 2013-05-02 12:21:06

0

用戶instanceof關鍵字檢查其是一個String對象還是Integer對象。