Can I change an item in a queue?

Say, if I have an integer queue (or any class T), can I change the value of an element in the queue? More specifically, if I define the queue as follows:

Queue<int> q = new Queue<int>();

Can we change the value of our element in the same way as we deal with an array? (if q was an array, we could do something like this: q[0]=1to change its element). I just wanted to simplify the script and use int as an example, but my intention was to look into the 1st element of class T in the queue, do some calculations and update the queue for other programs to process. I do not want to cancel it, because the sequence in the queue will not be the same as the original. Hope what I'm trying to do. Please inform.

+5
source share
6 answers

If the item in the queue was a changed type, you can change the value that the queue has in the first item. Without re-creating the queue or executing a large number of queues / dequeues, there is no way to change which item is in front of the queue.

As an example of the first case, if you have Queue<MyClass>a definition:

class MyClass
{
    public string Value { get; set; }
}

Queue<MyClass> queue = new Queue<MyClass>();
queue.Enqueue(new MyClass() { Value = "1" });
queue.Peek().Value = 2;
string value = queue.Peek().Value; // is 2
+6
source

You cannot directly modify an element in Queue(although you can use a workaround as Tudor suggested ). But if you want to have a queue, you do not need to use Queue. Another possible type from .Net is LinkedList. It allows you to add and remove things from both ends that can be used in your script:

LinkedList<int> list = new LinkedList<int>();

// enqueue an item
list.AddLast(1);

// dequeue an item
var item = list.First.Value;
list.RemoveFirst();

// put item back to the front of the queue
list.AddFirst(item);

, , . , ​​. . , .

: , , " " ( object s, - ).

TPL Dataflow ( .Net 4.5) . , , .

+4

:

class Wrapper<T>
{
    public T Value { get; set; }
}

static void Main(string[] args)
{
    Queue<Wrapper<int>> q = new Queue<Wrapper<int>>();
    Wrapper<int> wr = new Wrapper<int> { Value = 1 };
    q.Enqueue(wr);

    Wrapper<int> wr1 = q.Peek();
    wr1.Value = 2;

    int value = q.Dequeue().Value;
    Console.WriteLine(value);
}
+1

, , , , . "2":

    public class MyClass
    {
        public int Value { get; set; }
    }

    static void Main(string[] args)
    {
        Queue<MyClass> q = new Queue<MyClass>();
        q.Enqueue(new MyClass { Value = 1 });
        var i = q.Peek();
        i.Value++;
        i = q.Peek();
        Console.WriteLine(i.Value);
    }
+1
    public static class Extensions
    {
        public static Queue<T> SetFirstTo<T>(this Queue<T> q, T value)
        {
            T[] array = q.ToArray();
            array[0] = value;
            return new Queue<T>(array);
        }
    }

Strictly this does not mutate the queue, so reassignment is required.

        [TestMethod]
        public void Queue()
        {
            var queue = new Queue<int>(new[]{1,2,3,4});
            queue = queue.SetFirstTo(9);
            Assert.AreEqual(queue.Peek(),9);
        }
+1
source

The simple answer is no. It is not part of the queue API object

http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

However, anything is possible, of course. You can write an extension method for this, but it will have to work with the API of the object and thus delete / complete all elements along with the change while maintaining order.

But if you want to do this, you treat the queue as a list, so why not use List?

-3
source

All Articles