嗨我有两个对象的ArrayList,我需要将它合并为一个列表。这是我的要求
我的第一个列表
利斯塔
{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 :(得分:5)
鉴于:
List<MyClass> listA, listB;
试试这个:
List<MyClass> union = new ArrayList<MyClass>();
union.addAll( listA );
union.addAll( listB );
答案 1 :(得分:3)
我不太确定你拥有的是ArrayList
(来自输出),但即便如此,如果它是一个实现Collection
接口的类,你可以使用{{ 1}}方法,独立于确切的类(只要对象属于同一类型)。
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#addAll(java.util.Collection,T ...)
答案 2 :(得分:1)
如果这些是ArrayList&lt;&gt;包含相同类型的对象,您可以使用:
list1.addAll(list2);
它应该可以正常工作。
答案 3 :(得分:0)
Type是数组的类型....
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;
}