从列表中删除旧值

发布于 2024-09-28 12:27:04 字数 318 浏览 3 评论 0原文

我有一个列表,我希望能够存储 20 个值。删除旧值的好方法是什么?一个更好的例子是,想象一下更改历史记录,我不希望能够存储 20 个最新更改,而较旧的更改则消失。

C# 中是否有特殊的功能可以让我这样做,或者我必须自己制作或使用“删除”功能。

EDIT1:好吧,存储 4000 - 10000 个值怎么样,突然间链表看起来很有吸引力。

编辑2:循环列表很好,但是,我不想循环我的旧值。

EDIT3:对于我的问题,随机访问不太重要,但顺序访问很重要。

I have a list that I want to be able to store 20 values. What would be a good approach to deleting older values. A better example would be, imagine a change history and I wan't to be able to store 20 latest changes, while older ones go away.

Is there a special thing in C# that will let me do that or do I have to either make my own or use the Remove function.

EDIT1: Alright, how about storing 4000 - 10000 values, suddenly a linked-list looks attractive.

EDIT2: Circular list is good BUT, I don't want to be able to loop my older values.

EDIT3: For my problem, random access isn't too important, but sequential access is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

茶底世界 2024-10-05 12:27:04

使用队列。每次入队时检查 size == 20 是否。如果是,则出列/弹出一个元素

use a Queue. each time you enqueue into the queue check if size == 20. If so, dequeue/pop one element

天生の放荡 2024-10-05 12:27:04

听起来你正在描述一个环形缓冲区。 如何编写高效的循环缓冲区Java 或 C# 可能会有所帮助。

Sounds like you're describing a ring buffer. How would you code an efficient Circular Buffer in Java or C# might be helpful.

攒一口袋星星 2024-10-05 12:27:04

您可以创建自己的列表类:

public class BoundedList<T> : Collection<T> {
    public int MaxSize { get; private set; }
    public BoundedList(int MaxSize) : base(new List<T>(maxSize)) {
        MaxSize = maxSize;
    }
    protected override void InsertItem(T item, int index) {
        base.InsertItem(item, index)
        if (Count > MaxSize)
            Remove(0);
    }
}

You can make your own list class:

public class BoundedList<T> : Collection<T> {
    public int MaxSize { get; private set; }
    public BoundedList(int MaxSize) : base(new List<T>(maxSize)) {
        MaxSize = maxSize;
    }
    protected override void InsertItem(T item, int index) {
        base.InsertItem(item, index)
        if (Count > MaxSize)
            Remove(0);
    }
}
童话 2024-10-05 12:27:04

没有内置的集合可以处理这个问题,但您可以轻松制作一个:

public class LimitedList<T> : List<T> {

  public new void Add(T item) {
    if (Count == 20) {
      RemoveAt(0);
    }
    base.Add(item);
  }

}

但请注意,当前仅处理使用 Add 方法添加项目时的限制,而不是其他方法(如 )添加范围插入。此外,正如 SLask 指出的那样,List 类并不是专门用于继承的(尽管它没有标记为 final),因此更健壮的实现将封装列表而不是继承它。

另请注意,删除大型列表中的第一项效率很低,但只要项目数少至 20 个,就没有问题。如果您需要更大的集合,LinkedList 可以更好地处理这个问题。

There is no built in collection that handles that, but you could easily make one:

public class LimitedList<T> : List<T> {

  public new void Add(T item) {
    if (Count == 20) {
      RemoveAt(0);
    }
    base.Add(item);
  }

}

Note though that this currently only handles the limit when items are added using the Add method, and not other methods like AddRange and Insert. Also, as SLask pointed out, the List<T> class isn't specifically intended to be inherited (although it's not marked as final), so a more robust implementation would encapsulate the list instead of inheriting from it.

Note also that it's inefficient to remove the first item in a large list, but with as few items as 20, there is no problem. A LinkedList would handle that better if you need a much larger collection.

荒芜了季节 2024-10-05 12:27:04

好吧,我终于有机会自己思考这个问题,并找到了一个很好的折衷方案,而不需要花费太多的努力。为什么不直接使用链表呢?

  1. 对于历史类型的实现,只需跟踪指针即可。
  2. 排序操作仍然是线性时间。
  3. 删除最后一个值可以在恒定时间内完成。

我想我没有提到随机访问并不是太重要......

Alright I finally got a chance to think about this problem myself and found a good compromise without going through too much effort. Why not just use a linked list.

  1. For a history type of implementation, just keep track of the pointer.
  2. It will still be linear time for sequencing operations.
  3. Removing the last value can be done in constant time.

I guess I failed to mention that random access wasn't too important...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文