C# quickest way to shift array

前端 未结 20 1313
礼貌的吻别
礼貌的吻别 2020-12-01 01:35

How can I quickly shift all the items in an array one to the left, padding the end with null?

For example, [0,1,2,3,4,5,6] would become [1,2,3,4,5,6,null]

Ed

20条回答
  •  时光说笑
    2020-12-01 02:15

    Couldn't you use a System.Collections.Generic.Queue instead of an array ?

    I feel like you need to perform actions on your value the discard it, thus using a queue seems to be more appropriate :

    // dummy initialization
            System.Collections.Generic.Queue queue = new Queue();
            for (int i = 0; i < 7; ++i ) { queue.Enqueue(i); }// add each element at the end of the container
    
            // working thread
            if (queue.Count > 0)
                doSomething(queue.Dequeue());// removes the last element of the container and calls doSomething on it
    

提交回复
热议问题