2013-05-02 43 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对象。