"Java Fundamentals-Stacks"

Source: Internet
Author: User

Tagged with: throw data ring sys CSDN and stat exce

1. How to write a stack in Java code
 Public classStack {int[] data; intmaxSize; inttop;
  
PublicStack (intmaxSize) { This. maxSize =maxSize; Data=New int[MaxSize]; Top=-1; }/*** Add data in turn *@paramdata to be added *@returnAdd is successful*/    Public BooleanPushintdata) {if(top+1==maxSize) {System.out.println ("stack is full!"); return false; } This. data[++top] =data; return true; }/*** Extract data from the stack *@returnthe data taken out*/    Public intPop ()throwsexception{if(Top==-1) {Throw NewException ("The Stack is empty!"); }return This. data[top--]; } Public Static voidMain (string[] args)throwsException {Stack stack=NewStack (1000); Stack.push (1); Stack.push (2); Stack.push (3); Stack.push (4); Stack.push (5); while(stack.top>=0) {System.out.println (Stack.pop ()); }}}

"Java Fundamentals-Stacks"

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.