Package Ch01;public class MyArray {//requires a real array private long [] arr;private int elements;//Number of data elements public MyArray () {arr = new LONG[50];} Public MyArray (int maxsize) {arr = new long[maxsize];} Method of adding data public void Insert (Long value) {arr[elements] = value;elements++;} The method that is displayed public void display () {System.out.print ("["); for (int i=0;i<elements;i++) {System.out.print (arr[i]+ "");} System.out.print ("]"); System.out.println ();} Find data method, find data based on value to find, return value of subscript public int search (Long value) {int i;for (i=0;i<elements;i++) {if (arr[i] = = value) {break;}} if (i = = elements) {return-1;} Else{return i;}} Find data, return value according to index public long get (int index) {//Determine if array is out of bounds if (Index < 0 | | Index >= elements) {throw new Arrayindexoutofbou Ndsexception ("Array Out of Bounds");} Else{return arr[index];}} Delete data public void Delete (int index) {if (Index >=elements | | Index < 0) {throw new ArrayIndexOutOfBoundsException (" Array out of Bounds ");} else{for (int i=index;i<elements;i++) {Arr[index] = arr[index+1];} The length of the array--elements--;}} Update data public void change (int index,inT newvalue) {if (Index >= elements | | Index < 0) {throw new ArrayIndexOutOfBoundsException ();} else {Arr[index] = NEWV Alue;}}}
Array Learning 1