我在Silverlight 3.0中创建应用程序在该应用程序中,我想实现类似的功能:我必须维护值的集合。我不断从一端添加该集合中的值,并从另一端删除值。我想我想维护3000个值的集合。如果我在该集合中添加一个值,那么应删除一个值,以便我只有3000个值的集合。我想使用“循环队列”那么循环队列的silverlight中是否有任何功能?或者是否有任何有效的逻辑而不是循环队列?请帮帮我。谢谢。
答案 0 :(得分:1)
我不熟悉任何特定术语'循环队列',但您可以轻松创建自己的:
public class CircularQuene<T> : List<T>
{
public CircularQuene(int maxCount)
{
count = maxCount;
}
new public void Add(T variable)
{
base.Add(variable);
if (Count > count)
{
RemoveAt(0);
}
}
private int count;
public int MaxCount
{
get
{
return count;
}
set
{
count = value;
}
}
}
有点粗糙,但应该适合你的需要。
答案 1 :(得分:1)
您可能希望使用内置类Queue
并在其周围实施包装:
public class CircularQueue
{
private int totalItems;
private Queue<object> queue = new Queue<object>();
public CircularQueue(int maxCount)
{
this.totalItems = maxCount;
}
/// <summary>
/// Get first object from queue.
/// </summary>
public object Dequeue()
{
// ToDo: You might want to check first if the queue is empty to avoid a InvalidOperationException
object firstObject = this.queue.Dequeue();
return firstObject;
}
public void EnQueue(object objectToPutIntoQueue)
{
if (this.queue.Count >= this.totalItems)
{
object unusedObject = this.queue.Dequeue();
// ToDo: Cleanup the instance of ununsedObject.
}
this.queue.Enqueue(objectToPutIntoQueue);
}
}