从Object ArrayList中分离Integer和String ArrayList

时间:2013-05-02 12:14:16

标签: java object arraylist

我有一个包含对象的arraylist。我有两个不同的arraylists,一个包含字符串,另一个包含整数。现在我需要从父列表中获取字符串和整数并将其放入新的两个arraylists中。 arraylists如下。

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

请帮忙。

2 个答案:

答案 0 :(得分:4)

您只需检查Object是String还是Integer,并将其放在右侧列表中。

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

答案 1 :(得分:0)

用户instanceof关键字,用于检查其是String对象还是Integer对象。