packageutils;/****************************************************************** This class ArrayList implements a contiguous block of data* using an array.**TODO : Please Complete the Following*1. Comment all steps in your methods used for its*implementation. Be clear and precise.*2. Your comments must clearly explain the step to*the satisfaction of the instructor (in brief).*3. Use JavaDoc Comments above each method.*****************************************************************/public classArrayListE>{//constantspublic static final intDEFAULT_CAPACITY=10;// these are the data fields listed in the lab documentationE[]data;// reference for an array of type Eintsize;// stores the number of occupied places in the array.publicArrayList(){this(DEFAULT_CAPACITY);}@SuppressWarnings("unchecked")publicArrayList(intcapacity){data= (E[])newObject[capacity];size=0;}public booleanadd(Eitem){// creating a placeholder for the current sizeintoldSize =size;// allows the size of array to grow if size > lengthensureCapacity(size+1);// appends to back of list = first vacant spotdata[size] = item;// increments size = updated number of occupied spotssize++;//returns true if size updated by 1returnsize== (oldSize +1);}private voidcheckIndex(intindex){if(index0|| index >=size){throw newIndexOutOfBoundsException("Invalid Index: "+ index);}}public voidclear(){size=0;}public booleancontains(String value){for(inti =0;isize;i++) {if(value !=data[i]) {continue;}return true;}return false;//NOT FOUND}@SuppressWarnings("unchecked")public voidensureCapacity(inttargetCapacity){// checks if needed capacity > current array lengthif(targetCapacity >data.length){// select the larger of the two (double length or target size)intnewCapacity = Math.max(targetCapacity,2*data.length+1);//make a new array with the larger sizeE[] newArray = (E[])newObject[newCapacity];//copy the values in to the new arrayfor(inti =0;idata.length;i++){newArray[i] =data[i];}// re-assign underlying array reference to new arraydata= newArray;}}public intget(intindex){return(int)data[index];}public intindexOf(intvalue){for(inti =0;isize;i++) {if(data[i] == value){returni;}}return-1;//NOT FOUND}// returns true if list has no occupied itemspublic booleanisEmpty(){returnsize==0;}public shortremove(intindex){for(inti = index;isize-1;i++) {data[i] =data[i +1];}size--;return0;}public shortset(intindex,String value){checkIndex(index);data[index] = value;return0;}public intsize(){returnsize;}//prints out the values in sequence from 0 to length - 1publicStringtoString(){if(isEmpty()){return"[]";}else{StringBuilder result =newStringBuilder(" ["+data[0]);for(inti =1;isize;i++) {result.append(", ").append(data[i]);}returnresult +"]";}}}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here