2012-07-10 595 views
0

嗨,我有两个对象的ArrayList,我需要将它合并为一个列表。这里是我的要求如何在java中将两个对象列表合并成一个

我firstList

利斯塔

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl} 

和数组listB

{sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, `thirdmonth=30}` 

,我需要的结果是

结果

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl, sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30} 

编辑!

其实我的两个名单是ArrayList中,所以我listA的将是

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl} 
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper} 

和名单B将

{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90} 
{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90} 

,其结果应该是

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl, sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30} 
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper, sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90} 

也就是说,每个我的拖拽列表中的一行必须添加我的行明智。如果我使用addAll功能的两个列表只是追加像这样

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl} 
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper} 
{sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30} 
{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90}. But i need to append the two list row wise. Is it possible? 
+0

耶它是对象的ArrayList。 – user359187 2012-07-10 13:20:18

+0

@LukasEder,''StaffFirstName = f2“':-) – aioobe 2012-07-10 13:20:40

+0

@LukasEder,啊。好点:-) – aioobe 2012-07-10 13:21:36

回答

1

如果这些是ArrayList中<>含有相同类型的对象,你可以使用这个:

list1.addAll(list2); 

,它应该正常工作为了什么你要。

4

考虑:

List<MyClass> listA, listB; 

试试这个:

List<MyClass> union = new ArrayList<MyClass>(); 
    union.addAll(listA); 
    union.addAll(listB); 
0

类型是阵列类型....

Type[] concat(Type[] listA, Type[] listB) 
    { 
     Type[] list= new Type[listA.length+listB.length]; 
     System.arraycopy(listA, 0, list, 0, listA.length); 
     System.arraycopy(listB, 0, list, listA.length, listB.length); 

     return list; 
    }