bounded stack implementation

一世执手 提交于 2019-12-13 05:01:03

问题


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;
}

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).

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.

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 ?


回答1:


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.

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

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.




回答2:


Yes, you'll need two constructors. One constructor with no parameters, and one with a parameter for the size:

public BoundedStack() {
    // you could set the array directly
    list = new Object[32];

    // however, it's better to call the other constructor using the this() syntax
    this(32);
}

public BoundedStack(int size) {
    list = new Object[size];
}

You don't need to track the size of the array separate from the array itself. You can simply use list.length to find the size of the array, in other words:

public int getSize() {
    return list.length;
}

Side comments: you don't need to say java.lang.Object you can simply use Object. Also by convention you should not start variable names with upper case letters.




回答3:


Do i have to use 2 constructors ? one with the paramater and one without ?

You don't have to do it that way, but that seems the most natural. For example:

    ...

    public static final int DEFAULT_BOUND = 32;

    private final Object[] elements;
    private int size;

    public BoundedStack() { this(DEFAULT_BOUND); }

    public BoundedStack(int bound) {
        // validate bound ...
        elements = new Object[bound];
        size = 0;
    }

    public int size() { return size; }

    public int getBound() { return elements.length; }

The other thing that is confusing you (I think) is that a bounded stack has at least two "sizes":

  • One "size" is the number of elements currently in the stack. If you are going to be consistent with the Collection framework, then that size should be called size, and it should be returned by a getter with the non-standard name size().

  • The second "size" is the bound; i.e. the maximum number of elements that you will allow to be in the stack. I'd recommend you call this the bound or upperBound, and that you provide a conventionally named getter for it.

  • There is also possibly a third "size", representing the amount of space currently allocated to the bounded stack. However, that should probably be internal; i.e. not exposed via a getter.

In the collection framework, the term "capacity" is used for an initial "sizing parameter". However, collections types that have a "capacity" parameter only treat it as a hint. What you want is a hard limit on the stack size, and (IMO) that warrants the use of a different name.

You should be scrupulous in your API and internals to distinguish between these two "sizes" ... or you will confuse the people using the API, and ultimately, yourself.



来源:https://stackoverflow.com/questions/26225898/bounded-stack-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!