Java-stack Source code Analysis and examples

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.