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"