First, the use of ArrayList
in C # is practically depreciated, it goes back to the times where there were no generics in the language. I recommend you use List<T>
that implements IEnumerable<T>
and, among other advantages, allows you to use Linq in the collection.
Taking advantage of this, I am going to give you a solution to your problem using the methods Zip
, Concat
and Skip
of Linq, which is what I would use:
//Lo primero convierto los ArrayLists a List<string> para mejor manejo
List<string> l1 = lista1.Cast<string>().ToList();
List<string> l2 = lista2.Cast<string>().ToList();
//Unimos las dos listas
List<string> zipped= l1.Zip(l2, (l, n) => l + new string(' ',5) + n)
.Concat(l1.Count() > l2.Count() ? l1.Skip(l2.Count) : l2.Select(x=> new string(' ', 6)+x).Skip(l1.Count))
.ToList();
//Finalmente, imprimimos los elementos de la lista resultante
zipped.ForEach(x=>Console.WriteLine(x));
Zip
joins the two collections discarding the elements that are left over if one has more. Subsequently we join with Concat
the missing elements of the collection that has more elements.