Data structure (Java language)--arraylist

Source: Internet
Author: User

The following is an implementation of the ArrayList generic class. To avoid confusion with classes in the class library named myArrayList, the main details are:

    1. The member variable contains the underlying array, the array capacity, and the current number of items stored in the myarraylist.
    2. Provides a mechanism to change the capacity of the underlying array. By getting a new array, copy the old array to the new array to change the capacity of the new array, allowing the virtual machine to reclaim the old array.
    3. Provides implementations of Get () and set ().
    4. Provides basic operations such as size (), IsEmpty (), and Clear (), as well as remove (IDX), and add (x) and add (IDX,X) operations. If the array size and capacity are the same, then these two add operations will expand capacity.
    5. Provides a class that implements the iterator interface. This class stores the subscript for the next item in the iteration sequence and provides the implementation of methods such as Next (), Hasnext (), and remove (). The iterator method returns a new instance of the class that implements the iterator interface directly.
Import Java.util.iterator;import Java.util.nosuchelementexception;public class myarraylist<anytype> Implements iterable<anytype> {private static final int default_capacity = 10;private int thesize;private anytype[] t Heitems;public myarraylist () {clear ();} public void Clear () {thesize = 0;ensurecapacity (default_capacity);} public int size () {return thesize;} public Boolean isEmpty () {return size () = = 0;} public void Trumtosize () {ensurecapacity (size ());} Public AnyType get (int idx) {if (idx < 0 | | idx >= size ()) {throw new ArrayIndexOutOfBoundsException ();} return THEITEMS[IDX];} Public AnyType Set (int idx, AnyType newval) {if (idx < 0 | | idx >= size ()) {throw new arrayindexoutofboundsexception ();} AnyType old = Theitems[idx];theitems[idx] = Newval;return old;} @SuppressWarnings ("unchecked") public void ensurecapacity (int newcapacity) {if (Newcapacity < size ()) {return;} anytype[] Old = Theitems;theitems = (anytype[]) new object[newcapacity];for (int i = 0; i < size (); i++) {Theitems[i] = Old[i];}} public void Add (AnyType x) {Add (Size (), x);} public void Add (int idx, AnyType x) {if (Theitems.length = = Size ()) {ensurecapacity (Size () * 2 + 1);} for (int i = size (); i > idx; i--) {theitems[i] = theitems[i-1];} THEITEMS[IDX] = x;thesize++;}  Public AnyType Remove (int idx) {AnyType Removeditem = theitems[idx];for (int i = idx; i < size ()-1; i++) {theitems[i] = Theitems[i + 1];} Thesize--;return Removeditem;} Public iterator<anytype> Iterator () {return new Arraylistiterator ();} Private class Arraylistiterator implements iterator<anytype> {private int current = 0;public Boolean Hasnext () {Retu RN Current < Size ();} Public AnyType Next () {if (!hasnext ()) {throw new Nosuchelementexception ();} return theitems[current++];} public void Remove () {MyArrayList.this.remove (--current);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure (Java language)--arraylist

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.