Do you really need to use an Array
? why cant you just use a Queue
inside a class you define and expose the methods you want and keep the logic internally inside that class.
Something like this answer here maybe?
EDIT:
I didn't realise you were looking for a circular queue with a fixed size.
I made an answer here where you can also test it by pressing run.
Here is the implementation i wrote for the answer in the link.
public class FixedSizedCircularQueue<T>
{
private int _maxSize;
private int _currentIndex;
private T[] _q;
public FixedSizedCircularQueue(int maxSize)
{
_maxSize = maxSize;
_currentIndex = 0;
_q = new T[_maxSize];
}
public void Enqueue(T obj)
{
ResetIndexIfMaxSizeExceeded();
_q[_currentIndex] = obj;
_currentIndex++;
//for debug
Console.WriteLine("index: " + _currentIndex + " object: " + obj);
}
private void ResetIndexIfMaxSizeExceeded()
{
if((_currentIndex % _maxSize) == 0) _currentIndex = 0;
}
}
And here is how you would use it
var testQueue = new FixedSizedCircularQueue<int>(10);
for(var i = 0; i < 20; i++)
{
testQueue.Enqueue(i + 1);
}
which would output this with the debug console.WriteLine()
function
index: 1 object: 1
index: 2 object: 2
index: 3 object: 3
index: 4 object: 4
index: 5 object: 5
index: 6 object: 6
index: 7 object: 7
index: 8 object: 8
index: 9 object: 9
index: 10 object: 10
index: 1 object: 11
index: 2 object: 12
index: 3 object: 13
index: 4 object: 14
index: 5 object: 15
index: 6 object: 16
index: 7 object: 17
index: 8 object: 18
index: 9 object: 19
index: 10 object: 20