Stack--Array implementation

Source: Internet
Author: User

Importjava.util.Arrays;Importjava.util.EmptyStackException; Public classArraystack<e> {    protectedObject[] Elementdata;//Array    protected intElementcount;//Number of elements    protected intCapacityincrement;//Number of expansions    Private Static Final LongSerialversionuid = 1224463164541339165L;  PublicArraystack () {}/*** add Element *@paramItem *@return     */     Publice push (E item) {intMincapacity = elementcount+1; if(Mincapacity-elementdata.length > 0){            //Expansion            intOldcapacity =elementdata.length; intNewcapacity = Oldcapacity + ((capacityincrement > 0)?capacityincrement:oldcapacity); if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; Elementdata=arrays.copyof (Elementdata, newcapacity); } Elementdata[elementcount++] =item; returnitem; }    /*** Delete element *@return     */     Public synchronizede pop () {e obj; intLen =size (); //get the element you want to deleteobj =Peek (); //do the delete operationElementdata[elementcount] =NULL; Elementcount--; returnobj; }     Public synchronizedE Peek () {//determine that the stack is not empty        intLen =size (); if(len = = 0)            Throw Newemptystackexception (); intindex = len-1; //prevent array subscript from crossing        if(Index >=Elementcount) {            Throw NewArrayIndexOutOfBoundsException (index + ">=" +elementcount);        } E obj; Obj=(E) Elementdata[index]; returnobj; }    /*** length *@return     */     Public synchronized intsize () {returnElementcount; }}

Stack--Array implementation

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.