public interface Stack
Resizable stack of any elements.
Stack is a restricted version (inheritor) of MutableArray
interface,
allowing only access to the top element.
Please keep in mind: this package does not contain classes
that implements Stack but not implements MutableArray
.
It means that the following operation usually works successfully:
void someMyMethod(Stack stack) { MutableArray a = (MutableArray)stack; ... // access to any non-top elements }
Of course, it is not an example of good programming style, and there are no guarantees that such operator will not throw ClassCastException. But such an operation is usually posssible. Please compare:
void someMyMethod(Array readOnlyArray) { MutableArray a = (MutableArray)readOnlyArray; ... // attempt to modify elements }
This code will throw ClassCastException, if the caller of someMyMethod
does not forget to use asImmutable()
method for
creating readOnlyArray argument.
If this stack elements are primitive values (byte, short, etc.),
the stack must implement one of
BitStack
, CharStack
, ByteStack
, ShortStack
,
IntStack
, LongStack
, FloatStack
, DoubleStack
subinterfaces.
In other case, the stack must implement ObjectStack
subinterface.
Objects, implementing this interface, are not thread-safe, but are thread-compatible and can be synchronized manually if multithread access is necessary.
AlgART Laboratory 2007–2014
Modifier and Type | Method and Description |
---|---|
long |
length()
Returns the number of elements in this stack.
|
java.lang.Object |
popElement()
Removes the element at the top of this stack and returns it,
or throws EmptyStackException if the stack is empty.
|
void |
pushElement(java.lang.Object value)
Appends value element to the top of this stack.
|
long length()
java.lang.Object popElement()
It this object is an AlgART array, impelementing MutableArray
interface,
and it is not empty, the same action may be performed by the following code:
Object result = array.getElement
(array.length()
-1); array.length
(array.length()
-1);
It is a low-level method.
For stacks of primitive elements, implementing one of corresponding interfaces
BitStack
, CharStack
, ByteStack
, ShortStack
,
IntStack
, LongStack
, FloatStack
, DoubleStack
,
we recommend to use more efficient equivalent method of that interfaces:
BitStack.popBit()
, CharStack.popChar()
,
ByteStack.popByte()
, ShortStack.popShort()
,
IntStack.popInt()
, LongStack.popLong()
,
FloatStack.popFloat()
, DoubleStack.popDouble()
.
For other stacks, implementing ObjectStack
,
we recommend to use ObjectStack.pop()
.
java.util.EmptyStackException
- if this stack is empty.void pushElement(java.lang.Object value)
It this object is an AlgART array, impelementing MutableArray
interface,
the same action may be performed by the following code:
array.length
(array.length()
+1); array.setElement
(array.length()
-1, value);
It is a low-level method.
For stacks of primitive elements, implementing one of corresponding interfaces
BitStack
, CharStack
, ByteStack
, ShortStack
,
IntStack
, LongStack
, FloatStack
, DoubleStack
,
we recommend to use more efficient equivalent method of that interfaces:
BitStack.pushBit(boolean)
, CharStack.pushChar(char)
,
ByteStack.pushByte(byte)
, ShortStack.pushShort(short)
,
IntStack.pushInt(int)
, LongStack.pushLong(long)
,
FloatStack.pushFloat(float)
, DoubleStack.pushDouble(double)
.
For other stacks, implementing ObjectStack
,
we recommend to use ObjectStack.push(Object)
.
value
- to be added to the top of this stack.java.lang.ClassCastException
- if it is a stack of primitive elements and value is not
a corresponding wrapped class (Boolean, Integer, etc.).java.lang.ArrayStoreException
- if it is a stack of non-primitive elements and value is not
an instance of the class of stack elements.TooLargeArrayException
- if the resulting stack length is too large for this type of stacks.
Maxim Vorobyev Maxim Vorobyev. Статьи, новости. www.facebook.com Юрий хованский ютуб Юрий хованский ютуб your trusted source for хованский. catalogyoutube.ru