read last value from stack (java) -
i created stack store values follows. need read last value stack without using "stack.pop".
import java.util.stack; stack stack = new stack(); stack.push("something"); stack.push("something new");
class stack
extends class vector
, has lastelement()
, firstelement()
method return last respectively first element in collection.
after doing test can confirm top of stack (the last element pushed in) last element, bottom of stack first element. note if such access necessary, might indicate need different data structure stack, depends on context. @ rate, vector allows random access.
if need last element definition without popping it, using peek()
suggested elliott in comment appropriate method.
Comments
Post a Comment