使用什么样的数据结构(固定长度)?

发布于 2024-12-12 19:51:04 字数 431 浏览 1 评论 0原文

我想知道如果我遇到以下问题我应该使用哪种数据结构(队列):

  • 队列必须具有动态分配的长度(例如512)。
  • 每个新值都保存在队列末尾。
  • 添加新值时,如果队列已满,则第一个值将被删除。如果我向完整队列添加 30 个新值,则前 30 个将自动删除。
  • 存储的数据类型可以是数组或其他简单对象。
  • 我需要能够使用循环快速检索值,始终按顺序(无随机访问)。

这样做的目的是拥有一个固定宽度的数据源,图形将扫描该数据源来绘制其曲线。

编辑:该图旨在显示在 Android 自定义视图上。我是否可以使用特定的长度来使循环速度更快?

EDIT2:添加“添加新值时,如果队列已满,第一个值将被删除。如果我添加 30 个新值到完整队列,则第 30 个值将被删除会被自动丢弃。”

I would like to know what kind of data structure (queue) I should use if I have the following problem:

  • The queue must have a dynamically assigned length (say, 512).
  • Every new value is saved at the end of the queue.
  • When a new value is added, the first is dropped if the queue is already full. If I add 30 new values to a full queue, the 30 first are automatically dropped.
  • The kind of data stored be arrays or some other simple object.
  • I need to be able to quickly retrieve the values using a loop, always in order (no random access).

The purpose of this is to have a fixed width data source that a graph will scan to draw its curve.

EDIT: This graph is meant to be shown on an Android custom View. Is there a specific length I could use that would make the looping thru this faster?

EDIT2: Added "When a new value is added, the first is dropped if the queue is already full. If I add 30 new values to a full queue, the 30 first are automatically dropped."

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

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

发布评论

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

评论(5

夏の忆 2024-12-19 19:51:04

如果堆栈的容量永远不会改变,我会使用数组。我会跟踪“第一个”节点的位置并继续将其环绕。

我还想指出,您的行为看起来更像是队列而不是堆栈。您确定堆栈是您想要的吗?

class RotatingQueue<E> {
    Object[] data; // can't do E[]
    final int maxSize;
    int size = 0; // starts empty
    int first = 0; // starts at the front, why not?

    RotatingQueue(int size) {
        this.maxSize = size;
        data = new Object[size];
    }

    E add(E e) {
        E old = (E)(data[first]);
        old[first++] = e;
        if(size < maxSize) size++;
        return old;
    }

}

If the capacity of the stack is never going to change, I would use an array. I would keep track of where the "first" node is and keep wrapping it around.

I'd also like to point out that you're behavior seems more like a queue than a stack. Are you sure the stack is what you want?

class RotatingQueue<E> {
    Object[] data; // can't do E[]
    final int maxSize;
    int size = 0; // starts empty
    int first = 0; // starts at the front, why not?

    RotatingQueue(int size) {
        this.maxSize = size;
        data = new Object[size];
    }

    E add(E e) {
        E old = (E)(data[first]);
        old[first++] = e;
        if(size < maxSize) size++;
        return old;
    }

}
撩人痒 2024-12-19 19:51:04

您绝对需要一个 循环缓冲区 - 具有模块化访问的基于数组的队列。您可以轻松修改实现以删除第一个元素而不是引发异常。

You definitelly need a Circular buffer - array based queue with modular access. You can easily modify the implementation to drop first elements instead of throwing an exception.

懵少女 2024-12-19 19:51:04

我会推荐 LinkedHashSet 用于唯一项或 ArrayList。

I would recommend LinkedHashSet for unique items or an ArrayList.

硬不硬你别怂 2024-12-19 19:51:04

听起来你需要... 一个堆栈

不,我只是开玩笑。 “堆栈”在这里并不是真正正确的术语,因为堆栈是“后进先出”,这意味着您将内容添加到顶部,然后以相反的顺序将其取出。

您真正需要的是 Deque 实现。它是一个允许在两端插入和删除的队列。我假设只有当队列已满时才需要删除值。你的描述并没有完全说明这一点。如果您在插入新值时总是删除值,那么您最终会得到一种在任何时候都只有一个元素的数据结构。

我不知道有自动限制项目数量的实现。 ArrayDeque 非常接近,但你会仍然需要检查插入的大小并从一开始就删除无关的元素。它提供了 Java 集合的典型iterator 方法,允许您循环遍历项目。对于这种类型的集合,顺序是有保证的。

It sounds like you need... a stack!

No, I'm just kidding around. "Stack" isn't really the right term here, since a stack is "last-in-first-out", meaning that you add stuff to the top and then take it off in reverse order.

What you really need is a Deque implementation. It's a queue that allows insertion and removal at both ends. I'm gonna assume values only need to be dropped when the queue gets full. Your description didn't make this entirely clear. If you'd always drop values when inserting new ones, you'll end up with a data structure that only has one element in it at any time.

I don't know of an implementation that automatically limits the number of items. ArrayDeque comes pretty close, but you'd still need to check the size on insertions and remove extraneous elements from the start. It offers the typical iterator method of Java collections that allows you to loop over the items. For a collection of this type the order is guaranteed.

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