Java data Structure array implementation-additions and deletions to check

Source: Internet
Author: User

1, delete principle:

Package Com.struct.array;       /** * @ Description Base Array * @ Project name Java_datastruct * @ Package Name Com.struct.array * @ class name Basicarray * @author
    Chenlin * @date June 20, 2011 PM 8:41:21 */public class Basicarray {//array private long arr[];
    The effective length of the array is private int elements;
    The maximum length of the array is private int max;
        Initializes public basicarray (int max) {this.max = max;
    arr = new Long[max];
            /** * Insert Data * @param value/public void Insert (Long value) {if (Elements > Max) {
        throw new ArrayIndexOutOfBoundsException ();
        } arr[elements] = value;
    Elements + +;
        /** * Show All data */public void display () {System.out.print ("[");
        for (int i = 0; i < elements i++) {System.out.print ("" + arr[i]);
    } System.out.println ("]"); /** * Find index based on value * @param value * @return/public int SearcH (Long value) {int i;
            for (i = 0; i < elements; i++) {//represent found, exit loop if (arr[i] = = value) {break;
        } if (i = = elements) {return-1;
        }else {return i; }/** * Find numeric value based on index * @param index * @return/public long get (int index) {if Dex > Elements | |
        Index < 0) {throw new arrayindexoutofboundsexception ();
    return Arr[index]; /** * Changes the value on Index * @param index * @param value/public void change (int index, Long value)
        {if (Index > elements | | Index < 0) {throw new arrayindexoutofboundsexception ();
    } Arr[index] = value; /** * Delete data on index * @param index */public void Delete (int index) {if (Index > Elemen ts | | Index < 0) {throw new arrayindexoutofboundsexceptIon ();
            }else {for (int i = index; i < elements; i++) {Arr[i] = arr[i + 1];
        } Elements--;
        } public static void Main (string[] args) {Basicarray array = new Basicarray (30);
        Array.insert (22);
        Array.insert (12);
        Array.insert (34);
        Array.insert (44);
        Array.insert (86);
        Array.insert (57);
        Display Array.display ();
        System.out.println ("======================================");
        Find System.out.println (Array.search (12));
        System.out.println ("======================================");
        Modification of Array.change (2, 50);
        Array.display ();
        System.out.println ("======================================");
        Delete Array.delete (3);
    Array.display (); }
}

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.