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

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -