有界堆栈实现 [英] bounded stack implementation

查看:63
本文介绍了有界堆栈实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手.我想创建一个有界堆栈,用户可以在构造函数中选择他想要在数组(堆栈)中有多少个元素.我的堆栈是一个对象数组.我创建了这样的东西:

I'm new to Java. I would like to create a bounded stack where a user can choose in the constructor how many elements he wants in his array (stack). My Stack is an array of objects. I created something like this:

public class BoundedStack {

    int Size;
    java.lang.Object[] List;

    public BoundedStack(int size) {

  this.Size = size;
}

如果我创建BoundedStack类的实例,那将很酷,将使用构造函数中Paramater的大小创建类型对象的数组.( int大小).

It would be cool if I create an instance of the class BoundedStack, an array of the type object would be created with the size of the Paramater in the constructor. (int size).

如果用户不想在构造函数中给出一个数字,而只是声明一个 BoundedStack 类的实例.List数组的默认值应为32个元素.

If the user doens't want to give in a number in the constructor and just declares an instance of the class BoundedStack. The List array should have a default value of 32 elements.

在这里无法正常工作.我必须使用2个构造函数吗?一个带参数,一个不带参数?我知道构造函数并不难编写代码...但是通过实例化大小和数组,我的脑子里有些东西……

Something is just not working over here. Do i have to use 2 constructors ? one with the parameter and one without ? I know the constructors are not that hard to code...But something is passin' my mind by instantiating the sizes and the array...

有人知道我如何解决此问题?

Does anyone know how I can fix this issue ?

推荐答案

是的,两个构造函数.一个人应该接受一个大小的论点.另一个不应带有任何参数,并应使用 this(32); 遵循另一个,以获取默认大小32.

Yes, two constructors. One should take a size argument; the other should take no arguments, and should defer to the other using this(32); to get a default size of 32.

顺便说一句, LinkedBlockingDeque 将是使用标准JDK类实现此功能的方式.

By the way, LinkedBlockingDeque would be the way of implementing this using standard JDK classes.

此外,您可能想更改术语.通常,容量是表示堆栈可容纳的最大元素数的字,而 size 将是堆栈中当前包含的元素数.

Also, you might want to change terminology. Usually capacity would be the word for the maximum number of elements the stack can hold, and size would be the number of elements currently on the stack.

这篇关于有界堆栈实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆