问题
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
Collectionframework, then that size should be calledsize, and it should be returned by a getter with the non-standard namesize().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
boundorupperBound, 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