Creating a fixed-size Stack

后端 未结 8 2378
执念已碎
执念已碎 2020-12-06 16:05

I want to create a Stack in Java, but fix the size. For example, create a new Stack, set the size to 10, then as I push items to the stack it fills up and when it fills up t

8条回答
  •  天命终不由人
    2020-12-06 16:51

    A LinkedBlockingDeque is one simple option. Use the LinkedBlockingQueue(int) constructor where the parameter is the your stack limit.


    As you observed, Stack and Vector model unbounded sequences. The setSize() method truncates the stack / vector. It doesn't stop the data structure from growing beyond that size.

提交回复
热议问题