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)