C # Make an output of 2 columns of 2 different arrangements

0

If I have this

ArrayList lista1 = new ArrayList() { "A", "A", "A" };
ArrayList lista2 = new ArrayList() { "B", "B", "B", "B" };

it is possible to achieve something like this from output

            A         B
            A         B    
            A         B
                      B

At first I thought it obviously with FOR and console.writeline ("{0}

asked by Shiki 15.05.2017 в 08:33
source

3 answers

3

This should help you:

ArrayList lista1 = new ArrayList() { "A", "A", "A" };
ArrayList lista2 = new ArrayList() { "B", "B", "B", "B" };
// Determinas cual es el tamaño maximo de los arrays para usar de limite en el for
int tamMax = lista1.Count > lista2.Count ? lista1.Count : lista2.Count;
for (int i = 0; i < tamMax; i++) {
    /* Antes de imprimir el valor de cada array compruebas de que
     * no se pasa del límite y si se pasa del límite simplemente
     * imprimes un espacio vacío
     * */
    if (i < lista1.Count)
        Console.Write(lista1[i]);
    else
        Console.Write(" ");
    // Esto es solo para separar las columnas
    Console.Write("\t");
    /* Realizas la misma comparación que con la primera lista
     * pues no siempre sabes cual de las 2 es las larga
     * */
    if (i < lista2.Count)
        Console.Write(lista2[i]);
    // Imprimes el fin de línea
    Console.Write("\n");
}
    
answered by 15.05.2017 / 09:24
source
2

You can try the following code:

ArrayList lista1 = new ArrayList() { "A", "A", "A" };
ArrayList lista2 = new ArrayList() { "B", "B", "B", "B" };

var l1 = lista1.Count;
var l2 = lista2.Count;
var maxL = Math.Max(l1, l2);

for (var i = 0; i < maxL; i++)
{
    var x = i < l1 ? lista1[i] : ' ';
    var y = i < l2 ? lista2[i] : ' ';
    Console.WriteLine($"            {x}         {y}");
}

The important thing is that the for goes to the maximum of the lengths of the two lists. Then at each iteration it is verified first if the variable i is within the range of lista1 or lista2 , otherwise to x or y will be assigned a blank space. Then you can use a normal Console.WriteLine to print the corresponding line.

  

Note If you do not use C # 6 or higher the line of WriteLine you could change it for this:

Console.WriteLine("            {0}         {1}", x , y);
    
answered by 15.05.2017 в 09:23
1

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.

    
answered by 15.05.2017 в 09:54