简体   繁体   English

memory如何分配给java中的Stack和Queue

[英]How memory is allocated for Stack and Queue in java

We know ArrayList uses dynamic array to store data.我们知道 ArrayList 使用动态数组来存储数据。 LinkedList uses linked list to store data. LinkedList 使用链表来存储数据。 So, for both these case we know how it works when there is a new element added or deleted(Memory wise).因此,对于这两种情况,我们都知道添加或删除新元素时它是如何工作的(内存方面)。 Now similarly, how memory is allocated for a Stack or Queue in java.现在类似地,如何为 java 中的堆栈或队列分配 memory。 What happens, at Memory level, when I add/delete element to/from Stack or Queue.当我在堆栈或队列中添加/删除元素时,在 Memory 级别会发生什么。

Well, Queue is an interface, so not much to say about that.好吧, Queue是一个接口,所以不多说。 There's a lot of implementations and different behaviours.有很多实现和不同的行为。 Anyway, for instance, ArrayBlockingQueue has a memory allocation similar to ArrayList .无论如何,例如, ArrayBlockingQueue具有类似于ArrayList的 memory 分配。 Anyway, there are two major groups (bounded and unbounded) but it will really depend on the implementation you choose.无论如何,有两个主要组(有界和无界),但这实际上取决于您选择的实现。

About Stack , the memory allocation is also pretty similar to ArrayList as it's a subclass of Vector and this is backed by an array.关于Stack , memory 分配也与ArrayList非常相似,因为它是Vector的子类,并且由数组支持。

java.util.Stack is a subclass of java.util.Vector which is nothing but an array. java.util.Stackjava.util.Vector的子类,它只是一个数组。 Now java.util.Queue is an interface and has various implementations which are divided into two primary categories:现在java.util.Queue是一个接口,并具有各种实现,分为两大类:

  1. bounded - backed by an underlying array of fixed size. bounded - 由一个固定大小的底层数组支持。 A common example is ArrayBlockingQueue一个常见的例子是ArrayBlockingQueue
  2. unbounded - backed by a linked list and hence theoretically infinite.无界 - 由链表支持,因此理论上是无限的。 A common example is LinkedBlockingQueue一个常见的例子是LinkedBlockingQueue

Please check the docs for Stack and Queue请检查 堆栈队列的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM