简体   繁体   中英

To separate Integer and String ArrayList from Object ArrayList

I have an arraylist containing Objects. I have two different arraylists, one containing strings and the other integers. Now I need to get the strings and integers rom the parent list and put it in the new two arraylists. The arraylists are as follows.

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

Please help.

You simply check if the Object is whether a String or an Integer and put it in the right list.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM