On the realization of the custom ArrayList

Source: Internet
Author: User
Tags int size

The use of arrays to achieve custom ArrayList, in addition to basic additions and deletions and other methods, but also added to the API used in some of the methods to facilitate better understanding of the implementation of ArrayList, the implementation has been explained, the main function of the test method, the code is as follows:

Java code

/** * Custom Generic list * @author Chenjiang * @param <E> * * public class Myarraylist<e> {object[] data=new obje ct[0];//the initial array, defines an array of object types to hold the data public static void main (String args[]) {//Create an array list of integer types Myarrayl  
        Ist<integer> my=new myarraylist<integer> ();  
        add element for (int i=1;i<=5;i++) {my.add (i);  
        }//my.removefromto (1, 3);  
        My.add (1);  
        My.turnarray (); SYSTEM.OUT.PRINTLN (the location of the first occurrence of the specified element is +my.)  
        IndexOf (1)); System.out.println ("The last occurrence of the specified element is" +my.)  
        LastIndexOf (1));  
        System.out.println ("The current length of the queue is" +my.size ());  
        My.print ();  
    SYSTEM.OUT.PRINTLN ("Acquired element" +my.get (1));  
        /** * The method of printing queue elements */public void print () {int length=data.length;  
        for (int i=0;i<length;i++) {System.out.print (data[i]+ ""); /** * Add Element Method * @param e * * Publicvoid Add (e e) {int length=data.length;//Gets the length of the current array object[] Dest=new object[length+1];//creates a new array  
        Copy the elements from the original array first for (int i=0;i<length;i++) {dest[i]=data[i]; dest[length]=e;//Add new element data=dest;//replace the original array with the new array}/** * method of inserting elements * @param Inde  
        X specified subscript * @param e inserted element */public void Insert (int index,e e) {int length=data.length;  
        Object[] Dest=new object[length+1];  
        for (int i=0;i<index;i++) {//The elements before the index are copied first dest[i]=data[i]; dest[index]=e;//add a new element to the index position for (int j=index;j<length;j++) {//Then copy the latter part of the element dest[j+1]=  
        DATA[J];  
    } data=dest; /** * Delete Element method * @param index specified subscript/@SuppressWarnings ("unchecked") public E Remove (i  
        NT index) {int length=data.length;  
        Object[] Dest=new object[length-1]; for (int i=0;i<index;i++) {//Copy the elements before the index dest[i]=data[i];  
        for (int j=index+1;j<length;j++) {//Skip index specified position dest[j-1]=data[j];  
        } data=dest; Return (E) data[index];//gets the removed element}/** * method to remove all elements * * public void RemoveAll () {objec  
        T dest[]=new Object[0];  
    Data=dest;  
    /** * Gets the length of the queue */public int size () {return data.length; /** * Gets the element of the specified subscript */@SuppressWarnings ("unchecked") public E getting (int index) {return  
    (E) Data[index];  
    /** * To determine if the queue is null/public Boolean IsEmpty () {return size () ==0;  
        /** * method to change the element of the specified subscript position * @param index * @param e/public void set (int index,e e) {  
        method int Length=data.length with methods like inserting;  
        Object[] Dest=new object[length]; for (int i=0;i<index;i++) {dest[I]=data[i];  
        dest[index]=e;//change the element for the specified position for (int j=index+1;j<length;j++) {dest[j]=data[j];  
    } data=dest; /** * Deletes the middle element at the beginning of a subscript position to the end of a subscript * @param the subscript * @param end of the start position  
        LIC void removefromto (int start,int end) {int length=data.length;  
        Object dest[]=new object[length-(End-start)-1];  
        for (int i=0;i<start;i++) {dest[i]=data[i];  
        for (int j=end+1;j<length;j++) {dest[j-(End-start) -1]=data[j];  
    } data=dest;  
        /** * Flip list/public void Turnarray () {int length=data.length;  
        int j=length-1;  
        Object temp;  
            for (int i=0;i<length/2;i++) {temp=data[j];  
            Data[j]=data[i];  
            Data[i]=temp;  
        j--; }}/** * The first occurrence of the specified element in the search list, if the element does not existElement returns-1 * @param obj/public int IndexOf (Object obj) {int length=data.length;  
            for (int i=0;i<length;i++) {if (data[i]==obj) {return i;  
    }} return-1;   
        /** * The last occurrence of the specified element in the search list, and returns-1 * @param obj If it does not exist * * The specified element/public int lastindexof (Object obj) {  
        int length=data.length;  
            for (int i=length-1;i>=0;i--) {if (data[i]==obj) {return i;  
    }} return-1; }  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.