Self-writing super arrays

Source: Internet
Author: User

1  PackageCom.lovo;2 3 Importjava.util.Arrays;4 5 /**6 * Super Array7  * 8  * @authorAdministrator9  * Ten  * @param<T> Generic Parameters One  */ A  Public classSuperarray<t> { -     Privatet[] array; -     Private intsize; the  -     /** - * Constructors for super arrays -      */ +      PublicSuperarray () { -          This(8); +     } A  at     /** - * Constructors for super arrays -      *  -      * @parama specifies the initial capacity of the container -      */ -@SuppressWarnings ("Unchecked") in      PublicSuperarray (intcapacity) { -Array = (t[])Newobject[capacity]; to     } +  -     /** the * add Element *      *  $      * @params refers to T-elementPanax Notoginseng      */ -      Public voidAdd (T s) { the         if(Size = =array.length) { +Array = arrays.copyof (array, Array.Length! = 0? array.length * 2 A: 1); the         } +array[size++] =s; -     } $  $     /** - * Delete Element -      *  the      * @params refers to T-element -      */Wuyi      Public voidRemove (T s) { the          for(inti = 0; i < size; ++i) { -             if(Array[i] = =s) { Wu                  for(intj = i; J < size; ++j) { -ARRAY[J] = array[j + 1]; About                 } $--size; -             } -         } -     } A  +     /** the * Delete elements according to subscript -      *  $      * @params refers to T-element the      */ the      Public voidRemoveAt (intindex) { the         if(Index < 0 | | index >size) { the             Throw NewIndexoutofboundsexception ("array subscript out of bounds" +index); -         } in          for(inti = 0; i < size; ++i) { the             if(i = =index) { the                  for(intj = i; J < size; ++j) { AboutARRAY[J] = array[j + 1]; the                 } the--size; the             } +         } -  the     }Bayi  the     /** the * Empty Super Array -      */ -      Public voidClear () { the          for(inti = 0; i < size; ++i) { theArray[i] =NULL; the         } theSize = 0; -     } the  the     /** the * Determine if the super array is empty94      *  the      * @returnThe super array is null to return true, otherwise false the      */ the      Public BooleanIsEmpty () {98  About         returnSize = = 0; -     }101 102     /**103 * Find elements104      *  the      * @paramindex find element sitting in subscript106      * @returnT-element107      */108      PublicT Gett (intindex) {109         if(Index < 0 | | index >size) { the             Throw NewIndexoutofboundsexception ("array subscript out of bounds" +index);111         } the         returnArray[index];113     } the  the     /** the * Get the capacity of the Super array117      * 118      * @returnCapacity Size119      */ -      Public intgetcapacity () {121         returnArray.Length;122     }123 124     /** the * Get the number of elements in the super array126      * 127      * @returnnumber of T-elements -      */129      Public intGetSize () { the         return  This. Size;131     } the 133}

Test code

1  PackageCom.lovo;2 3 4  Public classTest02 {5      Public Static voidMain (string[] args) {6superarray<string> x =NewSuperarray<string> (1);7X.add ("card Sardinia");8X.add ("Apple");9X.add ("DJK");TenX.add ("Song Defu"); OneX.add ("Water and electricity"); A          for(inti = 0; I < x.getsize (); i++) { -System.out.print (X.gett (i) + ""); -         } the System.out.println (); -          - System.out.println (X.getsize ()); - System.out.println (X.getcapacity ()); + System.out.println (); -          +X.remove ("Banana"); AX.remove ("Received"); at System.out.println (X.getsize ()); -          - x.clear (); - System.out.println (X.getsize ()); - System.out.println (X.getcapacity ()); -          in         Try { -X.removeat (100); to}Catch(Exception e) { + System.out.println (E.getmessage ()); -         } the  *     } $}

Self-writing super arrays

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.