Java Data Structure Series--arrays (1)

Source: Internet
Author: User

Import Javax.management.runtimeerrorexception;public class MyArray {private long array[];p rivate int elements;// Used to record the number of actual data in the array public MyArray () {array=new long[50];//array default length is 50;}public MyArray (int capacity) {//sets the default length of the array array=new Long[capacity];} /** * Increment element at end of array */public void Add (Long data) {if (elements>array.length) {throw new ArrayIndexOutOfBoundsException ();} array[elements]=data;elements++;} /** * Insert element at index data */public void Insert (int index,long data) {if (index>=array.length| | index<0) {throw new Indexoutofboundsexception ();} if (elements>array.length-1) {throw new ArrayIndexOutOfBoundsException ();} for (int i=elements;i>=index;i--) {array[i+1]=array[i];} array[index]=data;elements++;} /** * Delete element at index * @param index */public void Delete (int index) {if (index>=array.length| | index<0) {throw new Indexoutofboundsexception ();} for (int i=index;i<=elements;i++) {array[i]=array[i+1];} elements--;} /** * Delete element data * @param data */public void Delete (Long data) {int addr=search (data); if (addr==-1) {System.out.println (data+ "does not Exist");} else{for (int i=addr;i<=elements;i++) {array[i]=array[i+1];} elements--;}} /** * Updates the element at index to NewData * @param index * @param newdata */public void update (int index,long newdata) {if (Index>=array . length| | index<0) {throw new Indexoutofboundsexception ();} Array[index]=newdata;} /** * Find element at index * @param index * @return */public long Search (int index) {if (index>=array.length| | index<0) {throw new Indexoutofboundsexception ();} return Array[index];} public int search (Long data) {int i;for (i=0;i<=elements;i++) {if (array[i]==data) {return i;}} return-1;} /** * Prints the class capacity in the array */public void display () {System.out.print ("["); for (int i=0;i<elements-1;i++) {System.out.print (array [i]+ ",");} System.out.print (Array[elements-1]); System.out.println ("]");}}

Java Data Structure Series--arrays (1)

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.