The
paper came to the end of shallow, I know this matter to preach-Lu
ask the canal that is clear so, for have fountainhead come--Zhu Xi
The stack is a stack, and the feature is Advanced (Filo,first in the last out). Stacks are inherited from vectors (vector queues), and because vectors are implemented in the same array, stacks are also passed through arrays rather than linked lists. The stack and collection relationships are as follows:
Stack Sample code:
Stack stack =NewStack ();
for(intI=1; i<6; i++)
{
Stack.push (string.valueof (i));//into the stack
}
intpos = Stack.search ("2");//Find
System.out.println ("The postion of 2 is:"+pos);
Stack.pop ();//out of the stack
String val = (string) Stack.peek ();//Stack Top Data
System.out.println ("Peek:"+val);
System.out.println (Stack.pop ());
System.out.println ("determine if the stack is empty, out of the stack");
while(!stack.isempty ())
{
System.out.println (Stack.pop ());
}
Result: The postion of 2 is:4peek:44 determine if the stack is empty, out of the stack 321
Stack source code in JAVA8:
Public classstack<E>extendsvector<E> {
PublicStack () {//Create an empty stack
}
PublicEPush(EItem) {//into the stack
AddElement (item);
returnItem;
}
//out of the stack
Public synchronizedEPop() {
EObj;
intlen = size ();
obj = Peek ();
Removeelementat (Len-1);
returnObj;
}
//returns the top element of the stack, but not the stack
Public synchronizedEPeek() {
intlen = size ();
if(len = =0)
throw NewEmptystackexception ();
returnElementAt (Len-1);
}
//determine if the stack is empty
Public BooleanEmpty() {
returnSize () = =0;
}
//finds the element and returns the stack depth
public synchronized intSearch(Object o) {
inti = LastIndexOf (o);
if(I >=0) {
returnSize ()-I;
}
return-1;
}
//Sequence Version number
private static final longSerialversionuid=1224463164541339165L;
}
Copyright Notice: Knowledge is to share, technology lies in communication, please leave a blogger's link on the reprint.
Java-stack Source code Analysis and examples