Implementation of the Stack Java

Source: Internet
Author: User

1  Packagepractice;2 3 ImportJava.util.Iterator;4 //Stack5  Public classMystack<t>ImplementsIterable<t> {6     Privatet[] A;7     Private intp = 0;8@SuppressWarnings ("Unchecked")9      PublicMystack () {TenA = (t[])NewObject[1]; One     } A      -      Public voidpush (T elements) { -         if(a.length = = p) {Resize (a.length);} thea[p++] =elements; -     } -      -      PublicT Pop () { +         if(A.LENGTH/4 = = p) {Resize (A.LENGTH/2);} -         returna[--p]; +     } A      at@SuppressWarnings ("Unchecked") -      Public voidResizeintlength) { -T[] B = (t[])NewObject[length];//change the length of the array -          for(inti = 0; I < P; i++) {//Copy the array -B[i] =A[i]; -         } inA = b;//let a point to B's memory space -     } to      +      Public BooleanIsEmpty () { -         returnp = = 0; the     } *      $      Public intsize () {Panax Notoginseng         returnp; -     } the @Override +      PublicIterator<t> Iterator () {//enables a data structure to allow foreach to access A         return Newarrayiterator (); the     } +     Private classArrayiteratorImplementsIterator<t>{ -          $         Private intP2 = 0; $          - @Override -          Public BooleanHasnext () {//return False Then foreach terminates the             returnP2! =p; -         }Wuyi  the @Override -          PublicT Next () {//foreach looks for the next element Wu             returna[p2++]; -         } About     } $}

Implementation of the Stack Java

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.