function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srikforcesrikforce 

How to add elements of List 1 to List 2

I have created List 1 and List 2



after creating objects to 2 diff classes I have added some elements.



List1.add(obj1); // adding obj1 to the List 1

List2.add(obj2) // adding obj2 to the List 2



Return type for both the Lists are different...



now I would like to return List 2 on debug logs but List 2 should contains elements of both List 1 and List 2...



Please try to resolve this..

thank u so much ,,,,,

Jia HuJia Hu

You can add List 1 and List 2 to a new List 3, as follows:

 

List<Account> ls1 = [Select name from Account limit 2];
System.debug('----------- ls1:' + ls1);
List<Contact> ls2 = [Select name from Contact limit 2];
System.debug('----------- ls2:' + ls2);
List<Sobject>ls3 = new List<Sobject>();
ls3.add((sobject)ls1[0]);
ls3.add((sobject)ls1[1]);
ls3.add((sobject)ls2[0]);
ls3.add((sobject)ls2[1]);
System.debug('----------- ls3:' + ls3);