2

Lets assume I have plenty of strings that need processing, I like to place the last processed string in memory to avoid repeated processing against it. I only need to record the last 100 strings, which means if I use

List<string> oldString after oldString.Add(), I have to use oldString.TakeFromEnd(100) As you know, TakeFromEnd() not exist, which means if I go this path, I have to write lots of things to keep a 100 length List which would lead to bad performance I can imagine.

I'd like to ask, is there any pre-made in system Class that just holds a fixed amount of data, and throws away oldest data when a new one is added. Thanks

[EDIT]

Queue<string> is very good indeed, use .Any() to check if already exist, use .Enqueue() to add (not Equeue as answered below, it shot a N), use .Count to check length, and .Dequeue() to remove the first added one.

4

1 に答える 1

7

キューを操作する

そして、アイデアは次のとおりです。

public void addToQueue(Object obj){
    if (myQueue.Count > 100)
        myQueue.Dequeue();

    myQueue.Equeue(obj);
}

これは大まかに使用する必要のあるコードのスケッチですが、アイデアは得られます。

次に、最新の100レコードのみを含むキューが作成されます。

于 2012-08-01T11:34:07.140 に答える