2013-11-27 73 views
1

我有一个ArrayList对象充满了一些ArrayList对象。一些条目是null并且我想只将的值不为空ArrayLists中的单个ArrayList。我在谈论下面的事情。从一些其他数组列表的列表中填充数组列表。

{null,[p1,p2,p3],[p2,p5,p6],[p4]} 

而最终需要的输出是这样的。

[p1,p2,p3,p2,p5,p6,p4] 

ArrayList中的ArrayList对象对象hash map object创建。我的代码段如下。但是它有什么问题。

Collection<ArrayList<MyProduct>> tmp = new ArrayList<ArrayList<MyProduct>>(); 

      tmp= orderAdap.values(); 

      ArrayList<MyProduct> flattenList = new ArrayList<MyProduct>(); 
      for(ArrayList<MyProduct> list : tmp){ 
       for(MyProduct i : list) 

        if(i!=null){ 
        lstStyle.add(i); 
        } 
      } 

那么有人可以帮助我。谢谢!!!!!

+0

“但它有什么问题......”请更具体一些。你是否试图将项目添加到'flattenList'? –

+0

最后想添加到ArrayList。 –

+0

请告诉我们,他输出的是什么情况,是否有错误被抛出? –

回答

2

做这样

tmp.remove(Collections.singleton(null)); 
for(ArrayList<MyProduct> list : tmp){   
    lstStyle.addAll(list); 
} 
0

使用addAll()

for(ArrayList<MyProduct> list : tmp) { 
    if(list!=null) 
     lstStyle.addAll(list); 
    } 
} 
0

看来你正试图从你的ArrayList列表只添加了独特的元素。我建议当你迭代你的列表时,在空检查后获得列表中的所有元素并将其添加到最终集合(例如hashset)。一旦你获得了集合(将包含来自所有阵列列表的独特元素),将其转换回列表。