【问题标题】:To separate Integer and String ArrayList from Object ArrayList将 Integer 和 String ArrayList 与 Object ArrayList 分开
【发布时间】:2013-05-02 12:14:16
【问题描述】:

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

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

请帮忙。

【问题讨论】:

    标签: java object arraylist


    【解决方案1】:

    您只需检查 Object 是 String 还是 Integer 并将其放入正确的列表中。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 2014-07-31
        • 2018-03-16
        • 2021-02-21
        • 1970-01-01
        • 2013-07-31
        • 2015-06-07
        相关资源
        最近更新 更多