How to display the content of a queue?

Asked

Viewed 596 times

1

I say this because I have created the Fila:

    Queue<string> Fila = new Queue<string>();    

But I can’t show it,

    string Pedido = (("Cliente:") + Cliente + (Environment.NewLine + "Produto:") + Produto + (Environment.NewLine + "Tamanho:") + Tamanho + (Environment.NewLine + "Quantidade") + Quantidade);
        //Adiciona o pedido a memória

The other variables above are like this:

    Cliente = Console.ReadLine(); //Insere na memória o que foi digitado pelo usuário.

In my attempt to display Fila content,

    if (resp == "S") //se a resposta for sim, exibe mensagem de sucesso e pergunta se deseja adicionar outro pedido
        {
            Fila.Enqueue(Pedido);
            Console.Write("Pedido Registrado! Deseja adicionar outro pedido? (S/N)");
            resp = Console.ReadLine();
            if (resp == "S")
            {
                Pedido = "";
                if (resp == "Verificar Fila")
                {
                    Console.Write("Fila: " + Fila);
                }

            }

What is displayed is:

    Fila.Collections.Generic.Queue`1[System.String]

2 answers

2

If you want a more compact mode:

Console.WriteLine(String.Join(Environment.NewLine, fila));

The static method String.Join joins items in a collection with a character (in this case line break).

Behold fuckinging here.

  • 1

    Yeah, that’s probably closer than he really wants.

1

Like Queue implements IEnumerable, you can iterate all elements with foreach:

Queue<string> fila = new Queue<string>();

fila.Enqueue("Teste 1");
fila.Enqueue("Teste 2");
fila.Enqueue("Teste 3");

foreach(string pedido in fila) {
    Console.WriteLine(pedido);
}

http://ideone.com/asyiVi

  • But in that case, would not print the request and not the order queue?

  • What do you mean by "print the queue"? The queue is the set of what’s in it.

  • Because I want to be able to print the queue, so in case you add another queue request later, I would print the whole queue (Request 1 + Request 2, etc)

  • So the foreach that I am suggesting sweeps the entire queue and prints the items one at a time. With this you mount the output you want.

  • I edited the example by placing more items in the queue, see: http://ideone.com/asyiVi

  • But consider that in the case my order is: string Order = (("Customer:") + Customer + (Environment.Newline + "Product:") + Product + (Environment.Newline + "Size:") + Size + (Environment.Newline + "Quantity") + Quantity); ///Add the order to memory

  • And the variables Client, Product, Size, etc follow the pattern: Client = Console.Readline(); //Inserts into memory what was typed by the user.

  • @Harison I could not continue the discussion at that moment. But Marcusvinicius' answer does not solve his problem?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.